项目中想加入地图功能,使用高德地图第三方,想要实现确定一个位置,搜索路线并且显示的方法。耗了一番功夫,总算实现了。

效果

WeChat_1462507820.jpeg

一、配置工作

1.申请key

访问 http://lbs.amap.com/dev/key/ 在高度地图第三方开发平台申请一个key,注册账户,新建应用,这个没什么门槛。
得到这个key

屏幕快照 2016-05-06 上午10.34.15.png

提示一下,这个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

屏幕快照 2016-05-06 上午11.02.06.png

屏幕快照 2016-05-06 上午10.48.19.png

二、地图基本显示

把地图添加到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高德地图使用-搜索,路径规划的更多相关文章

  1. [python] A*算法基于栅格地图的全局路径规划

    # 所有节点的g值并没有初始化为无穷大 # 当两个子节点的f值一样时,程序选择最先搜索到的一个作为父节点加入closed # 对相同数值的不同对待,导致不同版本的A*算法找到等长的不同路径 # 最后c ...

  2. 关于ArcGis for javascript整合百度地图 天地图 高德地图进行搜索

    1.ArcGis for javascript整合百度地图搜索 1.1.首先引入百度地图的api <!-- 引入百度地图API --> <script type="text ...

  3. customizable route planning 工业界地图产品的路径规划

    https://www.microsoft.com/en-us/research/publication/customizable-route-planning/?from=http%3A%2F%2F ...

  4. iOS - 高德地图步行线路规划多点多条线路

    项目集成高德地图遇到的问题: 高德地图的官方步行导航只针对单个起始点单条线路,驾车导航才有途径点多线路.现在项目是要步行导航多个点多条线路

  5. iOS:高德地图的使用

    本人花了点时间集成了高德地图的几乎所有的功能,包含:地图的显示.地图的绘制.地图的定位.地图的POI数据检索.地图的线路规划.地图导航等下载地址如下:https://github.com/xiayua ...

  6. IOS高德地图逆地理编码定位+网络判断

    先说下这功能的流程,  流程:判断用户是否联网--->获取用户地理位置经纬度--->通过经纬度去查询地理位置名称 //高德地图 @property (nonatomic, strong) ...

  7. iOS高德地图让指定区域或者点显示在屏幕中间

    对于高德地图也是一个新手,很多功能看文档,问技术 或者高德群里讨论  群号:204668425 在我们需求中绘制的有 圆 折线 不规则图形 方式,打开地图指定的绘制图形置于屏幕中间 1.首先创建一个数 ...

  8. iOS 高德地图轨迹回放的 思路, 及方法

    // 开始,公司要求制作一段跑步轨迹 在地图上的 动画回放, 传入一段经纬度, 开始一想,这不是很简单吗, 高德地图有可以把经纬度转换成坐标点的方法 /** * @brief 将经纬度转换为指定vie ...

  9. iOS高德地图SDK定位和搜索附近信息的具体使用

    1.显示地图.定位.显示当前位置. 导入你需要的功能的头文件,申明全局变量,代理方法等等.   初始化地图,在控制器即将显示额时候打开定位和跟踪用户,这里对参数不懂的话康忙进去都有注释.   对了.i ...

随机推荐

  1. ceph iscsi (SCST)

    ceph结合iscsi iscsi Target 安装 1.安装SCST tar -jxf scst-3.0.1.tar.bz2 cd scst-3.0.1 make && make ...

  2. 基于Amoba实现mysql主从读写分离

    一.Amoeba简介           Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy.它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特 ...

  3. C# 连接 mySQL 出现 GUID 应包含带 4 个短划线的 32 位数 问题

    C# 连接 mySQL 出现 GUID 应包含带 4 个短划线的 32 位数 问题 在连接字符串中加入 Old Guids=true; 如:server=localhost;userid=root;p ...

  4. CUDA 中的计时方法

    问题描述:一般利用CUDA进行加速处理时,都需要测试CUDA程序的运行时间,来对比得到的加速效果. 解决方法: 1).GPU端计时,即设备端计时. 2).CPU端计时,即主机端计时. 设备端计时有两种 ...

  5. [转]Java的文件读写操作

    file(内存)----输入流---->[程序]----输出流---->file(内存) 当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader,InputStr ...

  6. 使用C# 链接 Mysql 显示??? 乱码 的解决方案

    下载以下文件 mysql-connector-odbc-5.3.6-win32.msi 下载并打开以下文件: mysql-installer-community-5.6.28.0.msi 安装mysq ...

  7. php分享三十二:php调试工具

    一:phpdbg http://phpdbg.com/

  8. JS验证邮箱格式是否正确 实例代码

    如何用js验证邮箱格式是否正确?分享一个例子.代码: /* *验证邮箱格式是否正确 *参数strEmail,需要验证的邮箱 */ function chkEmail(strEmail) { if (! ...

  9. Unity创建一个简易的弹簧(弹动)效果

    参考文章:http://www.cnblogs.com/SkyD/archive/2008/09/05/1284778.html 主要依据胡克公式F=-k·x.这里k是倔度系数,可以理解为k值越大弹性 ...

  10. AdminLTE, Color Admin

    AdminLTE, Color Adminhttps://github.com/almasaeed2010/AdminLTE/http://www.seantheme.com/color-admin- ...