##准备工作 导入百度地图SDK (需要的是定位功能和检索功能的SDK)
##搞起
为了方便在多个页面调用 我们可以写一个单例。 新建一个类继承自 NSObject
上代码:.h文件
//定位成功的回调 包含海拔 经纬度信息
typedef void(^AFuserLocationCompletion)(BMKUserLocation *userLocation);
//实际的位置信息 省 市
typedef void(^AFReverseGeoCodeResultCompletion)(BMKGeoCodeSearch *searcher,BMKReverseGeoCodeResult *result,BMKSearchErrorCode error);
@interface AFMapManager : NSObject<BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{
BMKLocationService *_locService;
BMKGeoCodeSearch *_searcher;
AFuserLocationCompletion _locationCompletion;
AFReverseGeoCodeResultCompletion _ResultCompletion;
}
+ (AFMapManager *)manager;
//获得经纬度
- (void)af_startUserLocationServiceWithCompletion:(AFuserLocationCompletion)completion;
//根据经纬度 获得具体地址
- (void)af_getAddressWithuserLocation:(BMKUserLocation *)userLocation
Completion:(AFReverseGeoCodeResultCompletion)completion;
注意要导入百度地图的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
好吧稍微讲解一下:
定义两个方法。
//获得经纬度
- (void)af_startUserLocationServiceWithCompletion:(AFuserLocationCompletion)completion;
第一个方法的作用是获得BMKUserLocation 的一个对象。 其中包含经纬度、海拔等一些信息。 我们下面的方法要根据经纬度获取用户的具体地址信息。
//根据经纬度 获得具体地址
- (void)af_getAddressWithuserLocation:(BMKUserLocation *)userLocation
Completion:(AFReverseGeoCodeResultCompletion)completion;
第二个方法就是得到具体的省份、城市等信息了。
看一下.m里面的实现
@implementation AFMapManager
+ (AFMapManager *)manager{
static AFMapManager *manager = nil;
AFDISPATCH_ONCE_BLOCK(^{
if (!manager) {
manager = [[[self class] alloc] init];
}
})
return manager;
}
- (instancetype)init{
if (self == [super init]) {
_searcher = [[BMKGeoCodeSearch alloc]init];
_searcher.delegate = self;
_locService = [[BMKLocationService alloc] init];
_locService.delegate = self;
}
return self;
}
- (void)af_startUserLocationServiceWithCompletion:(AFuserLocationCompletion)completion{
_locationCompletion = [completion copy];
[_locService startUserLocationService];
}
- (void)af_getAddressWithuserLocation:(BMKUserLocation *)userLocation Completion:(AFReverseGeoCodeResultCompletion)completion{
_ResultCompletion = [completion copy];
//发起反地理编码
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude};
//逆向编码对象
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
//设置需要转化的经纬度
reverseGeocodeSearchOption.reverseGeoPoint = pt;
//发送逆向地理编码--》会回调 onGetReverseGeoCodeResult
BOOL flag = [_searcher reverseGeoCode:reverseGeocodeSearchOption];
if(flag)
{
NSLog(@"反geo检索发送成功");
}
else
{
NSLog(@"反geo检索发送失败");
}
}
#pragma mark - BMKLocationServiceDelegate
- (void)willStartLocatingUser {
NSLog(@"location start");
return;
}
/**
*在停止定位后,会调用此函数
*/
- (void)didStopLocatingUser {
NSLog(@"user location stop");
return;
}
/**
*用户方向更新后,会调用此函数
*@param userLocation 新的用户位置
*/
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation {
//NSLog(@"user derection change");
return;
}
/**
*用户位置更新后,会调用此函数
*@param userLocation 新的用户位置
*/
// 地理位置变更信息
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
NSLog(@"didUpdateUserLocation lat %f,long %f",
userLocation.location.coordinate.latitude,
userLocation.location.coordinate.longitude);
if (_locationCompletion) {
_locationCompletion(userLocation);
}
[_locService stopUserLocationService];
return;
}
/**
*定位失败后,会调用此函数
*@param error 错误号
*/
- (void)didFailToLocateUserWithError:(NSError *)error {
NSLog(@"定位失败");
if (_locationCompletion) {
_locationCompletion(nil);
}
[_locService stopUserLocationService];
return;
}
#pragma - 反向地理编码回调
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
if (_ResultCompletion) {
_ResultCompletion(searcher,result,error);
}
}
@end
使用方法
在需要定位到地址的文件里面导入头文件
#import "AFMapManager.h"
开始定位
[[AFMapManager manager] af_startUserLocationServiceWithCompletion:^(BMKUserLocation *userLocation) {
[[AFMapManager manager] af_getAddressWithuserLocation:userLocation Completion:^(BMKGeoCodeSearch *searcher, BMKReverseGeoCodeResult *result, BMKSearchErrorCode error) {
NSLog(@"地址是 >>%@",result.address);
}];
}];
这样就可以得到用户的地址了。 省、市、街道的信息也可以单独取出来。
/// 城市名称
result.addressDetail.city;
/// 区县名称
result.addressDetail.district
/// 省份名称
result.addressDetail.province//
/// 街道名称
result.addressDetail.streetName//
/// 街道号码
result.addressDetail.streetNumber//
有兴趣的话可以了解一下 BMKReverseGeoCodeResult 这个类
///反地址编码结果
@interface BMKReverseGeoCodeResult : NSObject
{
BMKAddressComponent* _addressDetail;
NSString* _address;
CLLocationCoordinate2D _location;
NSArray* _poiList;
}
///层次化地址信息
@property (nonatomic, strong) BMKAddressComponent* addressDetail;
///地址名称
@property (nonatomic, strong) NSString* address;
///商圈名称
@property (nonatomic, strong) NSString* businessCircle;
///地址坐标
@property (nonatomic) CLLocationCoordinate2D location;
///地址周边POI信息,成员类型为BMKPoiInfo
@property (nonatomic, strong) NSArray* poiList;
代码地址:https://github.com/AlwaysYang/AFMapManager