您的位置:首页 > 移动开发 > IOS开发

iOS 学习笔记五 【2016年百度地图定位详细使用方法】

2016-03-29 11:44 225 查看
具体介和配置绍在此就不详述了,详情请看百度地图API文档,

在这里具体讲解下,新版百度地图的定位与反地理编码的使用:

[b]1、导入头文件[/b]

#import <BaiduMapAPI_Map/BMKMapComponent.h>

#import <BaiduMapAPI_Location/BMKLocationComponent.h>

#import <BaiduMapAPI_Search/BMKSearchComponent.h>


2、设置代理

<

BMKLocationServiceDelegate,

BMKGeoCodeSearchDelegate

>


3、添加相关属性

@property (nonatomic, strong) BMKLocationService *locService;

@property (nonatomic, strong) BMKGeoCodeSearch *geoCode;        // 地理编码

@property (nonatomic, assign) CGFloat longitude;  // 经度

@property (nonatomic, assign) CGFloat latitude; // 纬度


4、开始写代码了

- (void)viewDidLoad {

[super viewDidLoad];

[self startLocation];

}


- (void)startLocation
{
NSLog(@"进入普通定位态");

// 初始化BMKLocationService
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
//启动LocationService
[_locService startUserLocationService];
}

#pragma mark - CoreLocation 代理
#pragma mark 跟踪定位代理方法,每次位置发生变化即会执行(只要定位到相应位置)
//实现相关delegate 处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
NSLog(@"heading is %@",userLocation.heading);
}

//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
NSLog(@"当前位置信息:didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

self.longitude = userLocation.location.coordinate.longitude;
self.latitude = userLocation.location.coordinate.latitude;

[self outputAdd];
// 当前位置信息:didUpdateUserLocation lat 23.001819,long 113.341650
}

#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.address.text = [NSString stringWithFormat:@"%@", result.address];
NSLog(@"位置结果是:%@ - %@", result.address, result.addressDetail.city);
//        NSLog(@"经纬度为:%@ 的位置结果是:%@", locationString, result.address);
self.currentCityString = result.addressDetail.city;

[self.cityButton setTitle:self.currentCityString forState:UIControlStateNormal];
// 定位一次成功后就关闭定位
[_locService stopUserLocationService];

}else{
NSLog(@"%@", @"找不到相对应的位置");
}

}

#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(@"%@", @"找不到相对应的位置");
}
}


最后打印结果:

跟踪定位代理方法

当前位置信息:didUpdateUserLocation lat 23.001882,long 113.341635

2016-03-29 11:33:48.126 yrapp[5462:1995584] 23.001882 - 113.341635

代理方法返回地理编码结果

位置结果是:广东省广州市番禺区汉溪大道东 -

好了,新版百度地图API就这么简单,弄了半个小时才弄好,使用过程中如有问题,请联系本人

最新demo:详见:[『BABaseProject』](https://github.com/boai/BABaseProject)中的`tabbar 的 消息栏目

微博:@博爱1616

QQ:137361770

## 3、博爱极力推荐

序号 | 类库 | 简介及功能介绍

:----------- | :-----------: | :-----------

3.1 | [『BAButton』](https://github.com/boai/BAButton) | 完全实现 UIButton 的自定义的类库。pod 导入:`pod 'BAButton', '~> 1.0.1'`

3.2 | [pod安装和使用方法](http://www.cnblogs.com/boai/p/4977976.html) | 对pod还是不熟的同学,可以看下我的博客,是最新的pod安装和使用方法,一直更新!

3.3 | [『BASegmentControl』](https://github.com/boai/BASegmentControl) | 新增网易新闻的滑动SegmentControl,基于[『HMSegmentedControl』](https://github.com/HeshamMegid/HMSegmentedControl)的完美二次封装!

3.4 | [『BAReminderDemo』](https://github.com/boai/BAReminderDemo) | 系统提醒和日历提醒,最近做了一个预约功能,有用到系统提醒和日历提醒,就写了这个demo!

3.5 | [『BALocalNotification』](https://github.com/boai/BALocalNotification) | 本地通知最新完美封装,最近整理了下本地通知和极光推送,有很多坑都踩过了,刚刚整理出来的完美封装,肯定适合大部分场合,也可以用此封装写闹钟,也提醒事件,都可以!如果喜欢,请在git上点个星吧!

3.6 | [『BANetManager』](https://github.com/boai/BANetManager) | 基于[『AFNetworking 3.1』](https://github.com/AFNetworking/AFNetworking)!最新版本的封装,集成了get/post 方法请求数据,单图/多图上传,视频上传/下载,网络监测 等多种网络请求方式!

3.7 | [『APP中的文字和APP名字的国际化多语言处理』](http://www.cnblogs.com/boai/p/5337558.html) | 最全、最贴心的国际化处理博客!

3.8 | 3D Touch 的纯代码实现方法 | 详见:[『BABaseProject』](https://github.com/boai/BABaseProject)中的`appdelegate`!

3.9 | [『DSAlert』](https://github.com/DS-Team/DSAlert-OC](https://github.com/DS-Team/DSAlert-OC) | 目前为止,最为精简的 alert 和 actionSheet 封装!DSAlert 让你的弹框不再孤单![『DSAlert』](https://github.com/DS-Team/DSAlert-OC](https://github.com/DS-Team/DSAlert-OC)!

3.10 | 最新、最全、最优美的友盟登录和分享的封装 | 详见:[『BABaseProject』](https://github.com/boai/BABaseProject)中的`demo 4`!

3.11 | 最新、最全、最优美的 清理 APP 缓存的封装 | 详见:[『BABaseProject』](https://github.com/boai/BABaseProject)中的`demo 2`!

3.12 | 最新、最全、最优美的 渐变navi 的封装 | 详见:[『BABaseProject』](https://github.com/boai/BABaseProject)中的`tabbar 的 消息栏目`!

3.13 | 最新、最全、最优美的 获取所有系统设置跳转 的封装 | 详见:[『BABaseProject』](https://github.com/boai/BABaseProject)中的`tabbar 的 消息栏目中demo 3`!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: