导语:
公司的项目中用到了百度地图的相关功能(定位、导航、自定义大头针)其中封装了几个百度地图的工具类,方便开发使用。
1.百度地位基础知识
由于系统原因,iOS不允许使用第三方定位,因此地图SDK中的定位方法,本质上是对原生定位的二次封装。通过封装,开发者可更便捷的使用。此外,地图SDK中还提供了相应的定位图层(支持定位三态效果),帮助开发者显示当前位置信息。
2.具体实现代码
创建一个定位工具管理类
.h文件
#import <Foundation/Foundation.h>
//定义一个block,定位成功后返回经纬度
typedef void(^LocationBlock)(NSString *lat,NSString *lon);
@interface BDLocationManager : NSObject
///当前位置的纬度
@property(nonatomic,copy)NSString *lat;
///当前位置的经度
@property(nonatomic,copy)NSString *lon;
///当前位置的地址信息
@property(nonatomic,copy)NSString *address;
+(instancetype)sharedManager;
///获取当前位置经纬度
-(void)getGps:(LocationBlock)block;
@end
.m文件
#import "BDLocationManager.h"
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <CoreLocation/CoreLocation.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
@interface BDLocationManager ()<BMKLocationServiceDelegate,UIAlertViewDelegate,BMKGeoCodeSearchDelegate>
///定义block
@property(nonatomic,copy)LocationBlock block;
/**
* 定位管理
*/
@property(nonatomic,strong)BMKLocationService *locService;
/**
* 地理编码
*/
@property(nonatomic,strong) BMKGeoCodeSearch *geoCode;
@end
@implementation BDLocationManager
+ (instancetype)sharedManager {
static BDLocationManager *_instance = nil;
static dispatch_once_t oncetoken;
dispatch_once(&oncetoken, ^{
_instance = [[BDLocationManager alloc] init];
});
return _instance;
}
- (instancetype)init {
if (self = [super init]) {
//初始化BMKLocationService
_locService = [[BMKLocationService alloc]init];
//设置定位精度
[_locService setDesiredAccuracy:kCLLocationAccuracyBest];
_locService.delegate = self;
_locService.distanceFilter = 30.f;
if ([CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服务可用");
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse||[CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedAlways) {
NSLog(@"用户授权");
} else if(status == kCLAuthorizationStatusDenied) {
//定位服务开启 --但是用户没有允许他定位
NSLog(@"用户未授权,跳转设置界面");
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"您的定位未经授权" message:@"PGS需要根据您的位置获取信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去设置", nil];
alertView.delegate=self;
[alertView show];
}
} else {
NSLog(@"去开启定位服务");
}
}
return self;
}
//获取当前的地理位置
- (void)getGps:(LocationBlock)block {
self.block = block;
//启动LocationService
[_locService startUserLocationService];
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
//获取到位置信息 赋值
[BDLocationManager sharedManager].lat = [@(userLocation.location.coordinate.latitude) stringValue];
[BDLocationManager sharedManager].lon = [@(userLocation.location.coordinate.longitude) stringValue];
self.block([@(userLocation.location.coordinate.latitude) stringValue],[@(userLocation.location.coordinate.longitude) stringValue]);
//初始化反地址编码选项(数据模型)
BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init];
//将TextField中的数据传到反地址编码模型
option.reverseGeoPoint= CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
//调用反地址编码方法,让其在代理方法中输出
if ([self.geoCode reverseGeoCode:option]) {
NSLog(@"反检索发送成功");
} else {
NSLog(@"反检索发送失败");
}
//关闭定位服务
// [_locService stopUserLocationService];
}
/**
*定位失败后,会调用此函数
*@param error 错误号
*/
- (void)didFailToLocateUserWithError:(NSError *)error {
NSLog(@"定位失败");
}
#pragma mark geoCode的Get方法,实现延时加载
- (BMKGeoCodeSearch *)geoCode {
if(!_geoCode) {
_geoCode= [[BMKGeoCodeSearch alloc] init];
_geoCode.delegate= self;
}
return _geoCode;
}
/**
*返回反地理编码搜索结果
*@param searcher 搜索对象
*@param result 搜索结果
*@param error 错误号,@see BMKSearchErrorCode
*/
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
//BMKReverseGeoCodeResult是编码的结果,包括地理位置,道路名称,uid,城市名等信息
if (error == BMK_SEARCH_NO_ERROR) {
//在此处理正常结果
[BDLocationManager sharedManager].address = result.address;
NSLog(@"位置:%@",[BDLocationManager sharedManager].address);
} else {
NSLog(@"抱歉,未找到结果");
}
}
//实现相关delegate 处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation {
// NSLog(@"处理方向变更信息 heading is %@",userLocation.heading);
}
#pragma mark 跳转设置界面
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:url]) {
NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}
}
}
@end
网友评论