iOS第三方地图-百度地图定位的封装
2015-08-26 17:22
381 查看
// // BaiduMapTools.h // baidumapTest // // Created by apple on 15/8/26. // Copyright (c) 2015年 tqh. All rights reserved. // #import <Foundation/Foundation.h> @interface WJBaiduMapTools : NSObject /**单例*/ +(WJBaiduMapTools *)instance; /**定位,能得到省市街道*/ - (void)startlocation:(BOOL)needaddress locationSuccess:(void(^)(double longitude,double latitude)) locationSuccess addressSuccess:(void(^)(double longitude,double latitude,BMKAddressComponent *address))addressSuccess; /**停止定位*/ - (void)stoplocation; @end
// // BaiduMapTools.m // baidumapTest // // Created by apple on 15/8/26. // Copyright (c) 2015年 tqh. All rights reserved. // #import "WJBaiduMapTools.h" //注:需要导入百度地图api @interface WJBaiduMapTools ()<BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate> { BMKLocationService *_locService; //定位 BMKGeoCodeSearch *_searcher; //geo搜索服务 BOOL _needaddress; //是否定位 //得到经纬度 void (^ _locationSuccess)(double longitude,double latitude); //得到经纬度和地理位置信息 void (^ _addressSuccess)(double longitude,double latitude,BMKAddressComponent *address); } @end @implementation WJBaiduMapTools +(WJBaiduMapTools *)instance { static WJBaiduMapTools *location; @synchronized(self) { if(!location) { location = [[WJBaiduMapTools alloc] init]; } } return location; } - (instancetype)init { self = [super init]; if (self) { _needaddress=NO; [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; [BMKLocationService setLocationDistanceFilter:100.f]; _locService = [[BMKLocationService alloc]init]; _locService.delegate = self; _searcher =[[BMKGeoCodeSearch alloc]init]; _searcher.delegate = self; } return self; } //开始定位 -(void)startlocation:(BOOL)needaddress locationSuccess:(void (^)(double, double))locationSuccess addressSuccess:(void (^)(double, double, BMKAddressComponent *))addressSuccess{ _needaddress=needaddress; _locationSuccess=locationSuccess; _addressSuccess=addressSuccess; if (_locService!=nil) { [_locService startUserLocationService]; } } //停止定位 - (void)stoplocation { if (_locService!=nil) { [_locService stopUserLocationService]; } } // 定位成功 -(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{ [_locService stopUserLocationService]; double longitude=userLocation.location.coordinate.longitude; double latitude=userLocation.location.coordinate.latitude; if (_locationSuccess) { _locationSuccess(longitude,latitude); } if (_needaddress) { //发起反向地理编码检索 BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init]; reverseGeocodeSearchOption.reverseGeoPoint = (CLLocationCoordinate2D){latitude,longitude}; BOOL flag = [_searcher reverseGeoCode:reverseGeocodeSearchOption]; if(flag) { NSLog(@"反geo检索发送成功"); } else { NSLog(@"反geo检索发送失败"); } }else{ } } -(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{ if (error == BMK_SEARCH_NO_ERROR) { if (_addressSuccess) { _addressSuccess(result.location.longitude,result.location.latitude,result.addressDetail); } } } @end
相关文章推荐
- iOS 使用xib后获取view的frame出错的问题
- ios 常用的一些终端命令行
- iOS APNs问题
- iOS中通知和代理的区别
- iOS-NSUserDefaults的用法(轻量级本地数据存储)
- IOS学习第七篇 ——Protocol协议
- iOS 9 学习系列:MapKit 的新变化(Flyover、交通和自定义)
- ios 设置状态栏风格
- 解决港版A1530 ios8 联通4G 电话打不进无法接通的问题,联通4G开关开启方法
- Nagios upgrade 3.x to 4.x 升级
- IOS NSUserDefaults 的用法
- [转]iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- iOS 万能跳转界面方法 (runtime实用篇一)
- 【iOS】配置和使用静态库
- iOS7之后的导航栏遮盖问题
- iOS开发 - 21.实用技术之获取文件的MIMEType类型
- IOS学习第六篇——代码块(block)
- iOS开发--TDD的iOS开发初步以及Kiwi使用入门
- IOS自带二维码生成
- ios copy 与 mutablecopy