iOS高德地图使用-搜索,路径规划
项目中想加入地图功能,使用高德地图第三方,想要实现确定一个位置,搜索路线并且显示的方法。耗了一番功夫,总算实现了。
效果
一、配置工作
1.申请key
访问 http://lbs.amap.com/dev/key/ 在高度地图第三方开发平台申请一个key,注册账户,新建应用,这个没什么门槛。
得到这个key
提示一下,这个key对应的bundle ID 要和工程里面的bundle ID 相同,不然每次打开地图都会报一个Invalid_user_scode的提示。
2.导入第三方
方便起见 pod导入
pod 'AMap3DMap' #3D地图SDK
#pod 'AMap2DMap' #2D地图SDK(2D地图和3D地图不能同时使用,2选1)
pod 'AMapSearch' #搜索服务SDK
3.打开工程的后台定位功能 更改info.plist
增加两条,一条请求位置时提示,一条https
二、地图基本显示
把地图添加到view即可显示
- (void)viewDidLoad {
[super viewDidLoad];
//配置用户Key
[MAMapServices sharedServices].apiKey = @"76bb9bc3718375ad03acba7c333694c4";
//把地图放在底层
[self.view insertSubview:self.mapView atIndex:0];
}
//地图懒加载
- (MAMapView *)mapView
{
if (!_mapView) {
_mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
_mapView.delegate = self;
_mapView.showsUserLocation = YES; //YES 为打开定位,NO为关闭定位
[_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:NO]; //地图跟着位置移动
//自定义定位经度圈样式
_mapView.customizeUserLocationAccuracyCircleRepresentation = NO;
//地图跟踪模式
_mapView.userTrackingMode = MAUserTrackingModeFollow;
//后台定位
_mapView.pausesLocationUpdatesAutomatically = NO;
_mapView.allowsBackgroundLocationUpdates = YES;//iOS9以上系统必须配置
}
return _mapView;
}
三、地图搜索功能
遵守协议 AMapSearchDelegate
高德提供了多种搜索方式,POI搜索(关键字查询、周边搜索、多边形查询),检索现实中真实存在的地物。
提示搜索,就是在还没有输入完全时,根据已有字符进行的搜索
//搜索框激活时,使用提示搜索
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
//发起输入提示搜索
AMapInputTipsSearchRequest *tipsRequest = [[AMapInputTipsSearchRequest alloc] init];
//关键字
tipsRequest.keywords = _searchController.searchBar.text;
//城市
tipsRequest.city = _currentCity;
//执行搜索
[_search AMapInputTipsSearch: tipsRequest];
}
//实现输入提示的回调函数
-(void)onInputTipsSearchDone:(AMapInputTipsSearchRequest*)request response:(AMapInputTipsSearchResponse *)response
{
if(response.tips.count == 0)
{
return;
}
//通过AMapInputTipsSearchResponse对象处理搜索结果
//先清空数组
[self.searchList removeAllObjects];
for (AMapTip *p in response.tips) {
//把搜索结果存在数组
[self.searchList addObject:p];
}
_isSelected = NO;
//刷新表视图
[self.tableView reloadData];
}
点击进行poi搜索
//周边搜索
- (IBAction)searchAction:(id)sender {
//初始化检索对象
_search = [[AMapSearchAPI alloc] init];
_search.delegate = self;
//构造AMapPOIAroundSearchRequest对象,设置周边请求参数
AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
//当前位置
request.location = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
//关键字
request.keywords = _searchController.searchBar.text;
NSLog(@"%@",_searchController.searchBar.text);
// types属性表示限定搜索POI的类别,默认为:餐饮服务|商务住宅|生活服务
// POI的类型共分为20种大类别,分别为:
// 汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|
// 医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|
// 交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
// request.types = @"餐饮服务|生活服务";
request.radius = 5000;//<! 查询半径,范围:0-50000,单位:米 [default = 3000]
request.sortrule = 0;
request.requireExtension = YES;
//发起周边搜索
[_search AMapPOIAroundSearch:request];
}
//实现POI搜索对应的回调函数
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
if(response.pois.count == 0)
{
return;
}
//通过 AMapPOISearchResponse 对象处理搜索结果
[self.dataList removeAllObjects];
for (AMapPOI *p in response.pois) {
NSLog(@"%@",[NSString stringWithFormat:@"%@\nPOI: %@,%@", p.description,p.name,p.address]);
//搜索结果存在数组
[self.dataList addObject:p];
}
_isSelected = YES;
[self.tableView reloadData];
}
四、路径规划
规划路径查询(驾车路线搜索、公交换成方案查询、步行路径检索),提前知道出行路线
//规划线路查询
- (IBAction)findWayAction:(id)sender {
//构造AMapDrivingRouteSearchRequest对象,设置驾车路径规划请求参数
AMapWalkingRouteSearchRequest *request = [[AMapWalkingRouteSearchRequest alloc] init];
//设置起点,我选择了当前位置,mapView有这个属性
request.origin = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
//设置终点,可以选择手点
request.destination = [AMapGeoPoint locationWithLatitude:_destinationPoint.coordinate.latitude longitude:_destinationPoint.coordinate.longitude];
// request.strategy = 2;//距离优先
// request.requireExtension = YES;
//发起路径搜索,发起后会执行代理方法
//这里使用的是步行路径
[_search AMapWalkingRouteSearch: request];
}
//长按手势响应方法,选择路径规划的终点,手势自己加
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
//在地图上长按的位置
CGPoint p = [gesture locationInView:_mapView];
NSLog(@"press on (%f, %f)", p.x, p.y);
}
//转换成经纬度
CLLocationCoordinate2D coordinate = [_mapView convertPoint:[gesture locationInView:_mapView] toCoordinateFromView:_mapView];
//赋值给目标点
_destinationPoint = [[MAPointAnnotation alloc] init];
_destinationPoint.coordinate = coordinate;
}
执行路径搜索后会执行代理方法
//实现路径搜索的回调函数
- (void)onRouteSearchDone:(AMapRouteSearchBaseRequest *)request response:(AMapRouteSearchResponse *)response
{
if(response.route == nil)
{
return;
}
//通过AMapNavigationSearchResponse对象处理搜索结果
NSString *route = [NSString stringWithFormat:@"Navi: %@", response.route];
AMapPath *path = response.route.paths[0]; //选择一条路径
AMapStep *step = path.steps[0]; //这个路径上的导航路段数组
NSLog(@"%@",step.polyline); //此路段坐标点字符串
if (response.count > 0)
{
//移除地图原本的遮盖
[_mapView removeOverlays:_pathPolylines];
_pathPolylines = nil;
// 只显⽰示第⼀条 规划的路径
_pathPolylines = [self polylinesForPath:response.route.paths[0]];
NSLog(@"%@",response.route.paths[0]);
//添加新的遮盖,然后会触发代理方法进行绘制
[_mapView addOverlays:_pathPolylines];
}
}
每次添加路线,区域,或者大头针等都会触发下面的代理方法
//绘制遮盖时执行的代理方法
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
/* 自定义定位精度对应的MACircleView. */
//画路线
if ([overlay isKindOfClass:[MAPolyline class]])
{
//初始化一个路线类型的view
MAPolylineRenderer *polygonView = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
//设置线宽颜色等
polygonView.lineWidth = 8.f;
polygonView.strokeColor = [UIColor colorWithRed:0.015 green:0.658 blue:0.986 alpha:1.000];
polygonView.fillColor = [UIColor colorWithRed:0.940 green:0.771 blue:0.143 alpha:0.800];
polygonView.lineJoinType = kMALineJoinRound;//连接类型
//返回view,就进行了添加
return polygonView;
}
return nil;
}
iOS高德地图使用-搜索,路径规划的更多相关文章
- [python] A*算法基于栅格地图的全局路径规划
# 所有节点的g值并没有初始化为无穷大 # 当两个子节点的f值一样时,程序选择最先搜索到的一个作为父节点加入closed # 对相同数值的不同对待,导致不同版本的A*算法找到等长的不同路径 # 最后c ...
- 关于ArcGis for javascript整合百度地图 天地图 高德地图进行搜索
1.ArcGis for javascript整合百度地图搜索 1.1.首先引入百度地图的api <!-- 引入百度地图API --> <script type="text ...
- customizable route planning 工业界地图产品的路径规划
https://www.microsoft.com/en-us/research/publication/customizable-route-planning/?from=http%3A%2F%2F ...
- iOS - 高德地图步行线路规划多点多条线路
项目集成高德地图遇到的问题: 高德地图的官方步行导航只针对单个起始点单条线路,驾车导航才有途径点多线路.现在项目是要步行导航多个点多条线路
- iOS:高德地图的使用
本人花了点时间集成了高德地图的几乎所有的功能,包含:地图的显示.地图的绘制.地图的定位.地图的POI数据检索.地图的线路规划.地图导航等下载地址如下:https://github.com/xiayua ...
- IOS高德地图逆地理编码定位+网络判断
先说下这功能的流程, 流程:判断用户是否联网--->获取用户地理位置经纬度--->通过经纬度去查询地理位置名称 //高德地图 @property (nonatomic, strong) ...
- iOS高德地图让指定区域或者点显示在屏幕中间
对于高德地图也是一个新手,很多功能看文档,问技术 或者高德群里讨论 群号:204668425 在我们需求中绘制的有 圆 折线 不规则图形 方式,打开地图指定的绘制图形置于屏幕中间 1.首先创建一个数 ...
- iOS 高德地图轨迹回放的 思路, 及方法
// 开始,公司要求制作一段跑步轨迹 在地图上的 动画回放, 传入一段经纬度, 开始一想,这不是很简单吗, 高德地图有可以把经纬度转换成坐标点的方法 /** * @brief 将经纬度转换为指定vie ...
- iOS高德地图SDK定位和搜索附近信息的具体使用
1.显示地图.定位.显示当前位置. 导入你需要的功能的头文件,申明全局变量,代理方法等等. 初始化地图,在控制器即将显示额时候打开定位和跟踪用户,这里对参数不懂的话康忙进去都有注释. 对了.i ...
随机推荐
- webpack window 使用sass来编译css样式
1.执行安装: npm install sass-loader --save-dev (此处不行的话就换上npm install node-sass) 2.稍微修改一下config,删掉我们先前添加的 ...
- sqlserver 2008 开启CLR
Common language runtime (CLR) 特性支持在sql server中编写和执行.net的存储过程.触发器.和函数但是要想执行CLR代码,首先要开启CLR特性 1.查看CLR特性 ...
- HTML5+CSS3整体回顾
http://blog.poetries.top/2016/10/19/HTML5+CSS3%E5%9F%BA%E7%A1%80%E5%9B%9E%E9%A1%BE%20/ 这篇文章主要总结H5的一些 ...
- sessionId与cookie 的关系(百度文库)
这篇文档讲的很清楚,推荐阅读 http://wenku.baidu.com/view/2ecf0b350b4c2e3f572763d1.html
- 基于TransactionScope类的分布式隐式事务
System.Transactions 命名空间中除了上一节中提到的基于 Transaction 类的显式编程模型,还提供使用 TransactionScope 类的隐式编程模型,它与显示编程模型相比 ...
- Latex中如何设置字体颜色(3种方式)
Latex中如何设置字体颜色(三种方式) 1.直接使用定义好的颜色 \usepackage{color} \textcolor{red/blue/green/black/white/cyan/ma ...
- 利用libsvm-mat建立分类模型model参数解密[zz from faruto]
本帖子主要就是讲解利用libsvm-mat工具箱建立分类(回归模型)后,得到的模型model里面参数的意义都是神马?以及如果通过model得到相应模型的表达式,这里主要以分类问题为例子. 测试数据使用 ...
- msf web_delivery模块攻击
目标机:win7 ip:192.168.31.136 攻击机:kai liunx ip:192.168.31.54 一. ...
- 如果使用EntityFramework6链接Mysql
web.config文件中加入这些: <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfigur ...
- kafka 集群的部署安装
这里我们罗列一下我们的环境 10.19.18.88 zk1 10.19.16.84 zk2 10.19.11.44 zk3 这里公司需要接入kafka用于zipkin来定位调用链 kafka 的地址是 ...