2017年百度地图定位详细使用方法

使用百度定位一定要先注册开发者账号,然后提出申请密钥(key) 才可使用该套SDK接口。

根据百度定位提供的API文档配置工程。配置链接:http://lbsyun.baidu.com/index.php?title=iossdk/guide/buildproject

下载百度定位的SDK和demo。

自己根据百度地图的demo,整理出来的一份定位用的。https://github.com/MinLee6/BMKOCDemo

写在前面的话首先在AppDelegate.mm文件中添加定位密钥

导入头文件并设置代理

#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
@interface AppDelegate : NSObject <UIApplicationDelegate, BMKGeneralDelegate>

创建定位管理对象

BMKMapManager* _mapManager;
开始编写代码( 替换成自己项目中的密钥

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
	// 要使用百度地图,请先启动BaiduMapManager
	_mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [_mapManager start:@"bP9bEpZ5WjmIUo10dQQGKo0EXcXxtvnh" generalDelegate:self];
	if (!ret) {
		NSLog(@"manager start failed!");
	}
    
    [self.window setRootViewController:navigationController];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)onGetNetworkState:(int)iError
{
    if (0 == iError) {
        NSLog(@"联网成功");
    }
    else{
        NSLog(@"onGetNetworkState %d",iError);
    }
    
}

- (void)onGetPermissionState:(int)iError
{
    if (0 == iError) {
        NSLog(@"授权成功");
    }
    else {
        NSLog(@"onGetPermissionState %d",iError);
    }
}


下面我讲一下使用定位的方法:

1:导入头文件

//地图定位
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>

2:设置代理

@interface LocationDemoViewController()<BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>

3:添加相关属性

@property (nonatomic, strong) BMKLocationService *locService;
// 地理编码
@property (nonatomic, strong) BMKGeoCodeSearch *geoCode;
// 经度
@property (nonatomic, assign) CGFloat longitude;
// 纬度
@property (nonatomic, assign) CGFloat latitude;
/* 当前城市名称 */
@property(nonatomic,copy)NSString *currentCityString;

4:详细内容代码

- (void)viewDidLoad {
    [super viewDidLoad];

    [self startLocation];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_locService stopUserLocationService];
    _locService.delegate = nil;
}

- (void)startLocation
{
    NSLog(@"进入普通定位态");
    
    // 初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    // 此处记得不用的时候需要置nil,否则影响内存的释放
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
}
/**
 *用户方向更新后,会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    NSLog(@"heading is %@",userLocation.heading);
}

/**
 *用户位置更新后,会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    NSLog(@"%@",userLocation.title);
    self.latitude = userLocation.location.coordinate.latitude;
    self.longitude = userLocation.location.coordinate.longitude;
    [self outputAdd];
}

/**
 *在地图View停止定位后,会调用此函数
 *@param mapView 地图View
 */
- (void)didStopLocatingUser
{
    NSLog(@"stop locate");
}

/**
 *定位失败后,会调用此函数
 *@param mapView 地图View
 *@param error 错误号,参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"location error");
}

#pragma mark geoCode的Get方法,实现延时加载
- (BMKGeoCodeSearch *)geoCode
{
    if (!_geoCode)
    {
        _geoCode = [[BMKGeoCodeSearch alloc] init];
        _geoCode.delegate = self;
    }
    return _geoCode;
}

//#pragma mark 获取地理位置按钮事件
- (void)outputAdd
{
    // 初始化反地址编码选项(数据模型)
    BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init];
    // 将数据传到反地址编码模型
    option.reverseGeoPoint = CLLocationCoordinate2DMake(self.latitude, self.longitude);
    NSLog(@"%f - %f", option.reverseGeoPoint.latitude, option.reverseGeoPoint.longitude);
    // 调用反地址编码方法,让其在代理方法中输出
    [self.geoCode reverseGeoCode:option];
}

#pragma mark 代理方法返回反地理编码结果
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (result) {
        
        self.currentCityString = [NSString stringWithFormat:@"%@%@",result.addressDetail.city,result.addressDetail.district];
         // 定位一次成功后就关闭定位
        [_locService stopUserLocationService];
        
        
    }else{
        NSLog(@"%@", @"找不到相对应的位置");
    }
    [_addressButton setTitle:self.address forState:UIControlStateNormal];
}

#pragma mark 代理方法返回地理编码结果
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (result) {
        NSString *locationString = [NSString stringWithFormat:@"经度为:%.2f   纬度为:%.2f", result.location.longitude, result.location.latitude];
        NSLog(@"经纬度为:%@ 的位置结果是:%@", locationString, result.address);
        //        NSLog(@"%@", result.address);
    }else{
        //        self.location.text = @"找不到相对应的位置";
        NSLog(@"%@", @"找不到相对应的位置");
    }
}
/**
 *定位失败后,会调用此函数
 *@param mapView 地图View
 *@param error 错误号,参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"location error");
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"无法定位" message:@"请在iPhone的\"设置-隐私-定位服务\"中允许灵佛使用定位服务。" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //
    }];
    [alertVC addAction:action];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值