为什么80%的码农都做不了架构师?>>>   

项目最近对地图整体模块进行了重构, 为了和我们的安卓同学保持统一,放弃了原本就很6的高德地图,全部改用百度地图(虽然我觉得百度地图不好用,文档也一般,但是没办法啊,没办法啊 啊啊啊啊啊..).

项目中用到的百度地图的主要功能点有以下几个:

  1. 基础地图和定位
  2. 反地理编码功能
  3. poi检索
  4. 搜索建议和poi详情检索
  5. 区域检索功能
  6. 通过url调起第三方地图进行
  7. 自定义弹出泡泡

在实际的使用过程中这些功能可能会有交叉,所以代码会整个贴过来,下面就根据实际功能需求一一介绍.

一.基础地图和定位功能

地图的初始化:

- (void)initMapView {self.mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_WIDTH/372*253)];self.mapView.showsUserLocation = YES;self.mapView.userTrackingMode = BMKUserTrackingModeNone;self.mapView.gesturesEnabled = YES;self.mapView.zoomEnabled = YES;self.mapView.maxZoomLevel = 23;self.mapView.zoomLevel = 16;// 显示比例尺 200m (和微信一样....)self.mapView.showMapScaleBar = YES;// 回到当前位置按钮UIButton *showUserLocation = [[UIButton alloc] initWithFrame:CGRectMake(self.mapView.mj_width - 21 - 50, self.mapView.mj_height - 21 - 50, 50, 50)];[self.mapView addSubview:showUserLocation];[showUserLocation addTarget:self action:@selector(backToCurrentLocation) forControlEvents:UIControlEventTouchUpInside];[showUserLocation setBackgroundImage:[UIImage imageNamed:@"showuserlocation"] forState:UIControlStateNormal];UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.mapView.mj_width * 0.5 - 8, self.mapView.mj_height * 0.5 - 40, 16, 40)];imageView.image = [UIImage imageNamed:@"datouzhen"];imageView.contentMode = UIViewContentModeScaleAspectFit;[self.mapView addSubview:imageView];[self.view addSubview:self.mapView];
}

这里提醒一点百度地图各项服务的delegate需要在 viewWillApper设置为self,viewWillDisapper时设置为nil,否则的话会出现内存泄露.

以下是定位成功后的回调方法

/***用户位置更新后,会调用此函数*@param userLocation 新的用户位置*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{[self.mapView updateLocationData:userLocation];CLLocation *location = userLocation.location;// 如果不是区域检索if (!(self.city.length > 0)) {// 设置当前位置为地图的中心点NSLog(@"%f-----%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) animated:YES];}self.currentCoor = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);// 定位成功会后if (location) {[self.locationService stopUserLocationService];// 反向编码[self beginReverseGeoCodeSearch];}
}

这里进行了判断,如果不是区域检索(如果我人在北京,但是要让地图显示是石家庄市 此时需要用到区域检索),则将当前定位到的位置设置为地图的中心点 . 然后开始进行反地理编码.

二.反地理编码

因为在发布分享的时候有一个选项显示的是当前所在的城市,如下图一,二  . 第二个cell显示的 "北京市" 所以需要进行反地理编码.

反地理编码成功后的回调:

// 反地理编码回调
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {//    result ->poiList ///地址周边POI信息,成员类型为BMKPoiInfo
//    @property (nonatomic, strong) NSArray* poiList;if (result.poiList.count > 0) {self.currentPoiInfo = [result.poiList firstObject];}// 反向地理编码成功后 ,进行poi搜索[self beginPoiSearch:self.currentCoor keyword:@""];
}

三.poi 检索

百度地图poi只能设置一个关键字(也有可能是我没找到方法,如果可以设置多个请及时联系,谢谢)

发起poi检索

#pragma mark ----发起检索
// 发起poi检索
- (void)beginPoiSearch:(CLLocationCoordinate2D)coor keyword:(NSString *)keyowrd{BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];option.pageCapacity = 50;// 按距离排序option.sortType = BMK_POI_SORT_BY_DISTANCE;// 以地图中心点为坐标发起检索 默认keyword 写字楼option.location = CLLocationCoordinate2DMake(coor.latitude, coor.longitude);option.keyword = keyowrd.length > 0 ? keyowrd : @"写字楼";BOOL flag = [self.poiSearch poiSearchNearBy:option];NSLog(@"%@",flag ? @"周边检索发送成功" : @"周边检索发送失败");
}

poi检索发起成功后的页面展示如下图:

        

图 一                                                                     图 二

这里除了poi检索之外,还加了一个地图中心点功能,及拖动地图的时候以大头针的位置为当前地图的中心点,然后再进行poi检索.这里需要监听另外一个回调方法.

// 拖拽地图的回调
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {[self beginPoiSearch:self.mapView.centerCoordinate keyword:self.keyword.length > 0 ? self.keyword : @""];
}

在这个回调方法中,以当前地图的中心为poi检索的中心点 发起poi检索.

四.搜索建议功能和poi详情检索

搜搜建议功能截图如下图:

在serchDisplayController的代理方法中发送搜索建议的请求:

#pragma mark ---displayControllerDelegate
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString {self.searchText = searchString;self.suggestSearch = [[BMKSuggestionSearch alloc]init];self.suggestSearch.delegate = self;BMKSuggestionSearchOption* option = [[BMKSuggestionSearchOption alloc] init];option.cityname = self.city;option.keyword  = searchString;BOOL flag = [self.suggestSearch suggestionSearch:option];NSLog(@"%@",flag ? @"建议检索发送成功" : @"建议检索发送失败");return YES;
}

然后在其回调方法中处理搜索建议结果:

// 搜索建议返回的关键词list和区list ,coor list,poi id list
@property (nonatomic,strong)NSMutableArray *keyList; // 万达广场
@property (nonatomic,strong)NSMutableArray *districtList; // 海淀区
@property (nonatomic,strong)NSMutableArray *coorList; // coor 包装成的 NSValue对象
@property (nonatomic,strong)NSMutableArray *poiuIdList; // poi id- (void)onGetSuggestionResult:(BMKSuggestionSearch *)searcherresult:(BMKSuggestionResult *)resulterrorCode:(BMKSearchErrorCode)error {self.keyList = [NSMutableArray arrayWithCapacity:0];self.districtList = [NSMutableArray arrayWithCapacity:0];self.coorList = [NSMutableArray arrayWithCapacity:0];self.poiuIdList = [NSMutableArray arrayWithCapacity:0];NSMutableArray *emptyLocationArray = [NSMutableArray arrayWithCapacity:0];for (int i = 0; i<result.ptList.count; i++) {CLLocationCoordinate2D coor;NSValue *coorValue = result.ptList[i];[coorValue getValue:&coor];// 非空的地址加到数组中if (!((int)coor.latitude == 0 && (int)coor.longitude == 0)) {[emptyLocationArray addObject:[NSString stringWithFormat:@"%d",i]];[self.keyList addObject:result.keyList[i]];[self.districtList addObject:result.districtList[i]];[self.coorList addObject:result.ptList[i]];[self.poiuIdList addObject:result.poiIdList[i]];}}[self.displayController.searchResultsTableView reloadData];
}

点击搜索建议tableView的cell时,需要mapView跳转到对应的位置,并搜索其附近对应的poi信息(比如我点的结果是个餐厅,那么就地图就滚动到这个餐厅的位置,并以"餐饮"为关键词搜索附近的poi信息),所以这里需要用到另外一个接口:poi详情搜索(注意是poi详情搜索,两个有点像)

点击cell时,根据poi 的id发起poi详情搜索 :

// 发起poi 详情检索
- (void)beginPoiDetailSearch:(NSString *)uid {BMKPoiDetailSearchOption* option = [[BMKPoiDetailSearchOption alloc] init];option.poiUid = uid;//POI搜索结果中获取的uidBOOL flag = [self.poiSearch poiDetailSearch:option];NSLog(@"%@",flag ? @"POI详情检索发送成功" : @"POI详情检索发送失败");
}

poi详情检索成功后的回调:

- (void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode {CLLocationCoordinate2D coor;NSValue *coorValue = self.coorList[self.searchResultSelectedIndexPath.row];[coorValue getValue:&coor];self.keyword = poiDetailResult.tag;// 改变中心坐标[self.mapView setCenterCoordinate:coor animated:YES];[self beginPoiSearch:coor keyword:poiDetailResult.tag];
}

返回的结果是 BMKPoiDetailResult 对象,里面包含了 poi的详细信息,包括 name address tag(poi标签)等.

然后以 poiDetailResult.tag 为关键字进行poi搜索.

五.区域检索功能

我人在北京,但是想查看石家庄的景点,小吃等信息 .  此时就需要用到区域检索功能了. 先判断

if (self.city.length > 0) {

[self beginDistrictSearch];

} 如果传过来的城市的名字长度大于 0 则说明要先进行区域检索.

// 发起区域检索
- (void)beginDistrictSearch {//初始化检索对象self.districtSearch = [[BMKDistrictSearch alloc] init];//设置delegate,用于接收检索结果self.districtSearch.delegate = self;//构造行政区域检索信息类BMKDistrictSearchOption *option = [[BMKDistrictSearchOption alloc] init];option.city = self.city;option.district = self.district;//发起检索BOOL flag = [self.districtSearch districtSearch:option];NSLog(@"%@",flag ? @"区域检索发送成功" : @"区域检索发送失败");
}

区域检索成功后的回调:

- (void)onGetDistrictResult:(BMKDistrictSearch *)searcher result:(BMKDistrictResult *)result errorCode:(BMKSearchErrorCode)error {NSLog(@"---->%f----%f",result.center.latitude,result.center.longitude);// 设置地图中心点[self.mapView setCenterCoordinate:result.center animated:YES];// 以城市中心点为中心发起poi检索[self beginPoiSearch:result.center keyword:@""];
}

这样就可以实现检索不同省份城市的 poi信息了.

六.通过url调起第三方地图进行

效果如下图:

      

点击上面导航按钮会进行判断,判断当前app是否安装了高德,百度以及苹果自带的地图 如果有则将其展示出来否则则不显示. 点击之后则会调起对应的地图进行导航,具体实现可以看我这篇博客 iOS通过URL调起第三方地图进行导航

七.自定义弹出pop

效果如下图:

点击大头针弹出pop ,这里的pop是自己绘制的代码:

@interface BubbleView : UIView@end@implementation BubbleView- (void)drawRect:(CGRect)rect {[super drawRect:rect];CGFloat width = rect.size.width;CGFloat height = rect.size.height;CGFloat radius = 5;CGFloat jianjiaoH = 12;// 获取CGContext,注意UIKit里用的是一个专门的函数CGContextRef context = UIGraphicsGetCurrentContext();// 移动到初始点CGContextMoveToPoint(context, radius, 0);// 绘制第1条线和第1个1/4圆弧CGContextAddLineToPoint(context, width - radius, 0);CGContextAddArc(context, width - radius, radius, radius, -0.5 * M_PI, 0.0, 0);//    CGContextAddArc(<#CGContextRef  _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#int clockwise#>)// 绘制第2条线和第2个1/4圆弧 17CGContextAddLineToPoint(context, width, height - radius - jianjiaoH);CGContextAddArc(context, width - radius, height - radius - jianjiaoH, radius, 0.0, 0.5 * M_PI, 0);// 绘制第3条线和第3个1/4圆弧和尖角CGContextAddLineToPoint(context, width/2 + 9, height - jianjiaoH);CGContextAddLineToPoint(context, width/2, height);CGContextAddLineToPoint(context, width/2 - 9, height - jianjiaoH);CGContextAddLineToPoint(context, width - radius, height - jianjiaoH);CGContextAddArc(context, radius, height - radius - jianjiaoH, radius, 0.5 * M_PI, M_PI, 0);// 绘制第4条线和第4个1/4圆弧CGContextAddLineToPoint(context, 0, radius);CGContextAddArc(context, radius, radius, radius, M_PI, 1.5 * M_PI, 0);// 闭合路径CGContextClosePath(context);// 填充半透明黑色CGContextSetRGBFillColor(context, 0, 0, 0.0, 0.5);CGContextDrawPath(context, kCGPathFill);
}@end

至此,此次我们用的百度地图的功能已经全部分享给大家了,如果纰漏请指正 .全部的代码我贴在动弹里了 -_-

转载于:https://my.oschina.net/zhxx/blog/748468

iOS百度地图的使用相关推荐

  1. ios 百度地图指定区域_ios百度地图的使用(普通定位、反地理编码)

    iOS定位 - 普通定位(没有地图) - 反地理编码(得到具体位置),下面通过代码给大家详解,代码如下: #import 使用到的头文件 要引入CoreLocation这个包 使用的代理名称 //1. ...

  2. iOS百度地图SDK之实时绘制轨迹(后台仍执行)

    首先,对于百度地图SDK的配置和环境搭建就不做说明,需要的人可以博客中另一篇文章看 <iOS百度地图SDK基本使用> ,本文的重点在于实现实时绘制轨迹的功能,并且对细节进行处理和优化 1. ...

  3. iOS 百度地图使用详解

    最近仿照美团做了款应用,刚好用到百度地图,高德地图之前用的比较多,只是这个项目的后台服务器是另外一个公司做的,他们用的就是百度地图,现在网上用百度地图的还不算太多,博文也是断断续续的,主要是中间跳跃有 ...

  4. iOS百度地图的相关开发(一)

    2019独角兽企业重金招聘Python工程师标准>>> 我是最近接触的百度读图开发,百度地图的相关sdk api开发写的很简单,有很多都是写的不是很详细,但是如果我们想深究就会遇到很 ...

  5. iOS 百度地图之坐标转换

    iOS系统定位是采用的是gps坐标,如果想在百度地图上展示有两种方式 1.百度地图SDK采用的是百度自有的地理坐标系(bdll09),因此开发者在做位置标注的时候,需要将其他类型的坐标转换为百度坐标. ...

  6. ios 百度地图指定区域_iOS百度地图简单使用详解

    百度地图 iOS SDK是一套基于iOS 5.0及以上版本设备的应用程序接口,不仅提供展示地图的基本接口,还提供POI检索.路径规划.地图标注.离线地图.定位.周边雷达等丰富的LBS能力 . 今天主要 ...

  7. iOS百度地图 Demo

    效果图 一.环境设置 1.开发环境:Xocode 7.3.1 2.模拟器环境:iOS 9.3 3.iOS 9 之后不能直接使用 HTTP 进行请求,需要在 Info.plist 新增一段用于控制 AT ...

  8. ios 百度地图指定区域_iOS开发(第三方使用)——百度地图的简单使用(定位与当前位置的显示)...

    使用cocoapods导入 pod 'BaiduMapKit' 在plist添加NSLocationAlwaysUsageDescription 去百度地图开发者中心注册帐号,并创建项目,拿到AK的值 ...

  9. ios 百度地图指定区域_获取百度地图可视区域范围的数据

    有个业务场景,需要根据获取到的地图区域显示,根据相应的经纬度反查 左侧区域的会议室. 思路: 1.得到百度地图可视区域--可视区域的中心点 2.可视区域的四个角的其中两个(东北角+西南角) http: ...

最新文章

  1. jQuery replaceWith replaceAll end的用法
  2. Django(part46)--form表单验证
  3. 群辉挂载linux nfs,Debian 9 挂载访问已设置的群晖NFS共享文件目录
  4. 设计模式之观察者模式在Listview中的应用
  5. html中的行内标签吗,HTML标签中行内元素和块级元素详解
  6. linux moxa 多串口卡_MOXA多串口卡C32030TCPU模块双RISC-based处理器架构大幅提升I/O数据传输的效能达到8口或以上...
  7. html 获取mac地址,Javascript获取Mac地址
  8. glibc之pthread_mutex_t/pthread_cond_t实现原理(十七)
  9. w ndoWs8pE模式下载,windows pe官方版下载_windows pe v8.2 - Win7旗舰版
  10. 国际贸易和计算机网络,网络对国际贸易的变革与影响.doc
  11. SystemUI 锁屏点击通知解锁界面重叠(bouncer界面与锁屏第一界面)
  12. Rasa_nlu_chi:入门教程
  13. php socket实践
  14. CVPR读书笔记[5]:Gabor特征提取之Gabor核的实现
  15. NSDictionary转换成JSON字符串
  16. Linux查看应用的CPU、内存使用情况
  17. 处理tophonetics的音标
  18. evo测试工具错误: evo module evo.main_traj crashed - no logfile written (disabled)
  19. A股日内回转交易意义何在?
  20. EmguCV安装配置

热门文章

  1. 图解Oracle同义词
  2. C#访问Access完整增删改查代码
  3. About me 留言板
  4. BZOJ 2820 YY的GCD 莫比乌斯反演
  5. python 3.x 学习笔记14 (socket_ssh and socket_文件传输)
  6. webpack使用和踩过的坑
  7. 二.无显示器远程连接树莓派
  8. 深入super,看Python如何解决钻石继承难题
  9. android不同Activity之间的数据共享
  10. 推送通知服务【WP7学习札记之十三】