iOS 调用地图导航
在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用来获取手机的系统,判断系统是多少
- CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;
- CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);
- if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下,调用google map
- NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];
- // @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude
- urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSURL *aURL = [NSURL URLWithString:urlString];
- [[UIApplication sharedApplication] openURL:aURL];
- } else { // 直接调用ios自己带的apple map
- MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
- MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
- toLocation.name = @"to name";
- [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
- launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
- }
如果不想使用苹果自带的地图的话,也可以使用第三方的地图,如百度、Google Maps、高德等
使用前,先判断设备上是否已安装应用
百度地图:
if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"baidumap://map/"]])
参考
高德地图:
if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"iosamap://"]])
参考
Google Maps:
if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"comgooglemaps://"]])
参考
示例代码
- - (void)availableMapsApps {
- [self.availableMaps removeAllObjects];
- CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;
- CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);
- NSString *toName = @"to name";
- if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
- NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=transit",
- startCoor.latitude, startCoor.longitude, endCoor.latitude, endCoor.longitude, toName];
- NSDictionary *dic = @{@"name": @"百度地图",
- @"url": urlString};
- [self.availableMaps addObject:dic];
- }
- if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
- NSString *urlString = [NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=applicationScheme&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=0&style=3",
- @"云华时代", endCoor.latitude, endCoor.longitude];
- NSDictionary *dic = @{@"name": @"高德地图",
- @"url": urlString};
- [self.availableMaps addObject:dic];
- }
- if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
- NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f?er=%f,%f&directionsmode=transit", endCoor.latitude, endCoor.longitude, startCoor.latitude, startCoor.longitude];
- NSDictionary *dic = @{@"name": @"Google Maps",
- @"url": urlString};
- [self.availableMaps addObject:dic];
- }
- }
显示一个ActionSheet
- [self availableMapsApps];
- UIActionSheet *action = [[UIActionSheet alloc] init];
- [action addButtonWithTitle:@"使用系统自带地图导航"];
- for (NSDictionary *dic in self.availableMaps) {
- [action addButtonWithTitle:[NSString stringWithFormat:@"使用%@导航", dic[@"name"]]];
- }
- [action addButtonWithTitle:@"取消"];
- action.cancelButtonIndex = self.availableMaps.count + 1;
- action.delegate = self;
- [action showInView:self.view];
实现delegate
- - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
- if (buttonIndex == 0) {
- CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;
- CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);
- if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下,调用google map
- NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];
- // @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude
- urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSURL *aURL = [NSURL URLWithString:urlString];
- [[UIApplication sharedApplication] openURL:aURL];
- } else{// 直接调用ios自己带的apple map
- MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
- MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil];
- MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:placemark];
- toLocation.name = @"to name";
- [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
- launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
- }
- }else if (buttonIndex < self.availableMaps.count+1) {
- NSDictionary *mapDic = self.availableMaps[buttonIndex-1];
- NSString *urlString = mapDic[@"url"];
- urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSURL *url = [NSURL URLWithString:urlString];
- DEBUG_LOG(@"\n%@\n%@\n%@", mapDic[@"name"], mapDic[@"url"], urlString);
- [[UIApplication sharedApplication] openURL:url];
- }
- }
iOS 调用地图导航相关推荐
- Android 调用地图导航
启动高德地图 /*** 启动高德App进行导航** @param sourceApplication 必填 第三方调用应用名称.如 amap* @param dname 非必填 目的地名称* @par ...
- iOS调用第三方导航和线路规划
线路规划: https://blog.csdn.net/qq_19979539/article/details/51938995 百度地图:baidumap: 高德地图:iosamap: 腾讯地图:q ...
- 微信中调用地图导航 可唤醒高德百度地图app(vue)
1.安装weixin-js-sdk npm i -S weixin-js-sdk 2 获取微信授权 import wx from 'weixin-js-sdk'async created() {//签 ...
- 微信中html调用地图导航 可唤醒高德百度地图app(vue)
1.安装weixin-js-sdk npm i -S weixin-js-sdk 2 获取微信授权 import wx from 'weixin-js-sdk' async created() { ...
- ios开发中如何调用苹果自带地图导航
前段时间一直在赶项目,在外包公司工作就是命苦,天天加班不说,工作都是和工期合同挂钩的,稍微逾期就有可能被扣奖金,不谈这些伤脑筋的事情了,让我们说说iOS开发中如何调用苹果手机自带的地图. 学习如逆水行 ...
- IOS 调用第三方地图APP导航
在开发中调用第三方APP进行路径规划,在此过程中不需要导入所调用地图的SDK,本文中只介绍调用百度地图.高德地图以及苹果自身地图APP. 在本项目中我用的是百度地图编码获取的百度经纬度坐标,而在开发过 ...
- ios百度导航SDK,iOS开发之百度地图导航
若遇到疑难问题可以去 ios导航SDK 论坛问题 查看和反馈:http://bbs.lbsyun.baidu.com/forum.php?mod=forumdisplay&fid=37 ...
- iOS Swift 应用内跳转第三方地图导航路线 及地图坐标系转换
支持百度 谷歌 高德 苹果 腾讯地图 一键打开及 地图之间的坐标系的转换 本项目 Demo 下载地址 https://github.com/sinorychan/JumpToThirdMap 注意 ...
- 百度地图no result available_【整理之路二】百度地图的路径规划和调用本机地图导航...
推荐看完之后注意一下最后的东西 一.细说百度地图的路径规划 路径规划主要有这么几种 1.公交路径规划 1.1 市内公交规划(暂时不在这里说) 1.2 跨市/省公交规划 // 导入头文件 #import ...
最新文章
- 《编译与反编译技术实战》——2.1节编译器、解释器及其工作方式
- java jsp w3c报错_JSP JavaBean
- 留言本的漏洞挖掘总结
- 开箱即用——用模板快速生成《客户意见反馈表》
- 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器,适配了iOS6-10系统,3行代码即可集成....
- leetcode 216. Combination Sum III | 216. 组合总和 III(Java)
- 前端学习(3342):ant design中grid排版使用
- 微信红包功能(含示例demo)
- mysql 特殊函数_mysql 的特殊函数
- SAP GUI 710 PATCH 12 下载地址
- 接口”安全机制”的设计
- 服务器被攻击ip显示美国,大神:服务器疑是被攻击,netstat命令看到连接有很多国外IP...
- 轻松读懂三极管,原来它是这样工作的
- 5369. 统计作战单位数
- 计算机应用责编处理录用几率大吗,等待责编处理是什么意思
- 借助Hugo和Academic主题在github.io建立个人网站
- 【地图导航】3D地图软件是如何做路径规划的?为什么准确率这么高
- cropped-p_large_a5mR_25c0000419eb2d0c.jpg
- 增强Spring @CacheEvict实现key模糊匹配清除
- 洛谷 P3387(缩点后+处理 )