ios 点生成线路 百度地图_iOS百度地图的使用
项目最近对地图整体模块进行了重构, 为了和我们的安卓同学保持统一,放弃了原本就很6的高德地图,全部改用百度地图(虽然我觉得百度地图不好用,文档也一般,但是没办法啊,没办法啊 啊啊啊啊啊..).
项目中用到的百度地图的主要功能点有以下几个:
基础地图和定位
反地理编码功能
poi检索
搜索建议和poi详情检索
区域检索功能
通过url调起第三方地图进行
自定义弹出泡泡
在实际的使用过程中这些功能可能会有交叉,所以代码会整个贴过来,下面就根据实际功能需求一一介绍.
一.基础地图和定位功能
地图的初始化:
- (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 *)searcher
result:(BMKSuggestionResult *)result
errorCode:(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
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搜索结果中获取的uid
BOOL 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(, , , , , , )
// 绘制第2条线和第2个1/4圆弧 17
CGContextAddLineToPoint(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
至此,此次我们用的百度地图的功能已经全部分享给大家了,如果纰漏请指正 .全部的代码我贴在动弹里了 -_-
ios 点生成线路 百度地图_iOS百度地图的使用相关推荐
- ios 点生成线路 百度地图_百度地图iOS SDK
iOS SDK v2.3.0 百度地图 iOS SDK是一套基于iOS4.3及以上版本设备的应用程序接口,不仅提供展示地图的基本接口,还提供POI检索.路径规划.地图标注.离线地图.定位等丰富的LBS ...
- ios 高德地图加载瓦片地图_iOS高德地图添加自定义瓦片地图
瓦片地图 首先解释一下什么是瓦片地图,我们使用的地图(例如百度,高德)都有一个底图,在每一级的缩放比例下,都有一张很大的底图,这张底图按固定的大小切割成若干份,在地图显示时根据显示范围和缩放比例,请求 ...
- ios 点生成线路 百度地图_iOS SDK | 百度地图API SDK
注意事项 1.静态库中采用ObjectC++实现,因此需要您保证您工程中至少有一个.mm后缀的源文件(您可以将任意一个.m后缀的文件改名为.mm),或者在工程属性中指定编译方式,即在Xcode的Pro ...
- ios 点生成线路 百度地图_iOS开发-集成百度地图(OC语言)
一.申请安全码 图1-1 申请安全码 二.导入SDK,添加依赖库 1.根据项目需求(定位?路径规划?)下载所需要的SDK包,传送门:下载百度地图SDK,并拖进项目中. 图2-1 导入SDK 2.添加依 ...
- ios 点生成线路 百度地图_网站地图全面解析
网站地图相信大家都不陌生,但对于一些刚刚接触seo的小伙伴来说可能会有些疑惑.这段时间我也时常听到有学员说网站地图怎么去制作分析,搞得也是非常的头痛,现在就来带大家全面透析网站地图吧. 一.什么是网站 ...
- ios 点生成线路 百度地图_百度地图下载-百度地图ios版15.3.0苹果版-东坡下载
百度地图ios版是一款非常好用的手机地图导航软件,独自出行又不熟悉路程?下载百度地图ios版一键搜索所达目的地,自动为您推荐最快方案.时间最短方案.转车最少方案和红绿灯最少方案.软件实用方便,是出行的 ...
- ios 高德地图加载瓦片地图_IOS 高德地图 API 加载 WMS 服务
IOS 高德地图 API 加载 WMS 服务 本文主要介绍通过自定义高德地图 MATileOverlay 接口,添加 WMS 服务到地图上.废话少说,先贴代码. 代码 自定义类 WMSTileOver ...
- ios 百度地图指定区域_iOS百度地图简单使用详解
百度地图 iOS SDK是一套基于iOS 5.0及以上版本设备的应用程序接口,不仅提供展示地图的基本接口,还提供POI检索.路径规划.地图标注.离线地图.定位.周边雷达等丰富的LBS能力 . 今天主要 ...
- ios 一步一步学会自定义地图吹出框(CalloutView)--(百度地图,高德地图,google地图)
前言 在ios上边使用地图库的同学肯定遇到过这样的问题:吹出框只能设置title和subtitle和左右的view,不管是百度地图还是高德地图还是自带的google地图,只提供了这四个属性,如果想添加 ...
最新文章
- AI算法在FPGA芯片上还有这种操作?| 技术头条
- oracle导入到mysql命令_oracle数据库导入导出命令
- 前端性能优化之jQuery按需加载轮播图
- 2005年存储市场关键词TOP10
- antd option宽度自适应_网站自适应模板是什么
- tomcat下list所有文件的目录
- [html] 写一个垂直的三栏布局,第一栏固定顶部,中间铺满,第三栏固定底部
- 将ProudCity建立为开放组织
- 使用charles修改服务器返回数据,charles_01_打断点修改接口请求返回数据
- python交互式帮助的进入、使用和退出_python退出交互式???
- 9 线性表-队列-链式存储
- 代码片段:基于 JDK 8 time包的时间工具类 TimeUtil
- Unix网络编程学习笔记
- (转)《C++ Qt 编程视频教程》(C++ Qt Programming)[MP4]
- NTFS, FAT32和exFAT文件系统有什么区别
- Python之灵魂三问,分享学习路上的神器集合!
- python—武汉市2021年新房数据分析
- lol排位服务器维护赢了没加分,LOL最新排位制裁,“挂机退游戏会被限制加分,力度将逐次叠加”...
- Julia 安装包报错操作超时
- 焦虑症应该怎么办?这六个缓解方法建议试试
热门文章
- springboot集成mongoDB高级聚合查询,关联查询,lookup.let多条件关联查询。
- 背单词软件-设计与实现
- 仓禀实而知礼节,衣食足而知荣辱
- 【解决】Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource
- 年月日转天数,天数转月日
- JavaScript的eval()方法的使用
- 【考研线代】三. 向量
- 3051压力变送器安装指南
- 好团队必有的7个特征
- 物联网盒子是什么?有什么用?