在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用来获取手机的系统,判断系统是多少

[cpp] view plaincopy
  1. CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;
  2. CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);
  3. if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下,调用google map
  4. 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];
  5. //        @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude
  6. urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  7. NSURL *aURL = [NSURL URLWithString:urlString];
  8. [[UIApplication sharedApplication] openURL:aURL];
  9. else { // 直接调用ios自己带的apple map
  10. MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
  11. MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
  12. toLocation.name = @"to name";
  13. [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
  14. launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
  15. }

如果不想使用苹果自带的地图的话,也可以使用第三方的地图,如百度、Google Maps、高德等

使用前,先判断设备上是否已安装应用

百度地图:

if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"baidumap://map/"]])

参考

高德地图:

if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"iosamap://"]])

参考

Google Maps:

if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"comgooglemaps://"]])

参考

示例代码

[cpp] view plaincopy
  1. - (void)availableMapsApps {
  2. [self.availableMaps removeAllObjects];
  3. CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;
  4. CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);
  5. NSString *toName = @"to name";
  6. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
  7. NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=transit",
  8. startCoor.latitude, startCoor.longitude, endCoor.latitude, endCoor.longitude, toName];
  9. NSDictionary *dic = @{@"name": @"百度地图",
  10. @"url": urlString};
  11. [self.availableMaps addObject:dic];
  12. }
  13. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
  14. NSString *urlString = [NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=applicationScheme&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=0&style=3",
  15. @"云华时代", endCoor.latitude, endCoor.longitude];
  16. NSDictionary *dic = @{@"name": @"高德地图",
  17. @"url": urlString};
  18. [self.availableMaps addObject:dic];
  19. }
  20. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
  21. NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f?er=%f,%f&directionsmode=transit", endCoor.latitude, endCoor.longitude, startCoor.latitude, startCoor.longitude];
  22. NSDictionary *dic = @{@"name": @"Google Maps",
  23. @"url": urlString};
  24. [self.availableMaps addObject:dic];
  25. }
  26. }

显示一个ActionSheet

[cpp] view plaincopy
  1. [self availableMapsApps];
  2. UIActionSheet *action = [[UIActionSheet alloc] init];
  3. [action addButtonWithTitle:@"使用系统自带地图导航"];
  4. for (NSDictionary *dic in self.availableMaps) {
  5. [action addButtonWithTitle:[NSString stringWithFormat:@"使用%@导航", dic[@"name"]]];
  6. }
  7. [action addButtonWithTitle:@"取消"];
  8. action.cancelButtonIndex = self.availableMaps.count + 1;
  9. action.delegate = self;
  10. [action showInView:self.view];

实现delegate

[cpp] view plaincopy
  1. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  2. if (buttonIndex == 0) {
  3. CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;
  4. CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);
  5. if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下,调用google map
  6. 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];
  7. //        @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude
  8. urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  9. NSURL *aURL = [NSURL URLWithString:urlString];
  10. [[UIApplication sharedApplication] openURL:aURL];
  11. else{// 直接调用ios自己带的apple map
  12. MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
  13. MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil];
  14. MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:placemark];
  15. toLocation.name = @"to name";
  16. [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
  17. launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
  18. }
  19. }else if (buttonIndex < self.availableMaps.count+1) {
  20. NSDictionary *mapDic = self.availableMaps[buttonIndex-1];
  21. NSString *urlString = mapDic[@"url"];
  22. urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  23. NSURL *url = [NSURL URLWithString:urlString];
  24. DEBUG_LOG(@"\n%@\n%@\n%@", mapDic[@"name"], mapDic[@"url"], urlString);
  25. [[UIApplication sharedApplication] openURL:url];
  26. }
  27. }

iOS 调用地图导航相关推荐

  1. Android 调用地图导航

    启动高德地图 /*** 启动高德App进行导航** @param sourceApplication 必填 第三方调用应用名称.如 amap* @param dname 非必填 目的地名称* @par ...

  2. iOS调用第三方导航和线路规划

    线路规划: https://blog.csdn.net/qq_19979539/article/details/51938995 百度地图:baidumap: 高德地图:iosamap: 腾讯地图:q ...

  3. 微信中调用地图导航 可唤醒高德百度地图app(vue)

    1.安装weixin-js-sdk npm i -S weixin-js-sdk 2 获取微信授权 import wx from 'weixin-js-sdk'async created() {//签 ...

  4. 微信中html调用地图导航 可唤醒高德百度地图app(vue)

    1.安装weixin-js-sdk npm i -S weixin-js-sdk 2 获取微信授权  import wx from 'weixin-js-sdk'  async created() { ...

  5. ios开发中如何调用苹果自带地图导航

    前段时间一直在赶项目,在外包公司工作就是命苦,天天加班不说,工作都是和工期合同挂钩的,稍微逾期就有可能被扣奖金,不谈这些伤脑筋的事情了,让我们说说iOS开发中如何调用苹果手机自带的地图. 学习如逆水行 ...

  6. IOS 调用第三方地图APP导航

    在开发中调用第三方APP进行路径规划,在此过程中不需要导入所调用地图的SDK,本文中只介绍调用百度地图.高德地图以及苹果自身地图APP. 在本项目中我用的是百度地图编码获取的百度经纬度坐标,而在开发过 ...

  7. ios百度导航SDK,iOS开发之百度地图导航

    若遇到疑难问题可以去   ios导航SDK 论坛问题  查看和反馈:http://bbs.lbsyun.baidu.com/forum.php?mod=forumdisplay&fid=37 ...

  8. iOS Swift 应用内跳转第三方地图导航路线 及地图坐标系转换

    支持百度 谷歌 高德 苹果 腾讯地图 一键打开及  地图之间的坐标系的转换 本项目 Demo 下载地址  https://github.com/sinorychan/JumpToThirdMap 注意 ...

  9. 百度地图no result available_【整理之路二】百度地图的路径规划和调用本机地图导航...

    推荐看完之后注意一下最后的东西 一.细说百度地图的路径规划 路径规划主要有这么几种 1.公交路径规划 1.1 市内公交规划(暂时不在这里说) 1.2 跨市/省公交规划 // 导入头文件 #import ...

最新文章

  1. 《编译与反编译技术实战》——2.1节编译器、解释器及其工作方式
  2. java jsp w3c报错_JSP JavaBean
  3. 留言本的漏洞挖掘总结
  4. 开箱即用——用模板快速生成《客户意见反馈表》
  5. 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器,适配了iOS6-10系统,3行代码即可集成....
  6. leetcode 216. Combination Sum III | 216. 组合总和 III(Java)
  7. 前端学习(3342):ant design中grid排版使用
  8. 微信红包功能(含示例demo)
  9. mysql 特殊函数_mysql 的特殊函数
  10. SAP GUI 710 PATCH 12 下载地址
  11. 接口”安全机制”的设计
  12. 服务器被攻击ip显示美国,大神:服务器疑是被攻击,netstat命令看到连接有很多国外IP...
  13. 轻松读懂三极管,原来它是这样工作的
  14. 5369. 统计作战单位数
  15. 计算机应用责编处理录用几率大吗,等待责编处理是什么意思
  16. 借助Hugo和Academic主题在github.io建立个人网站
  17. 【地图导航】3D地图软件是如何做路径规划的?为什么准确率这么高
  18. cropped-p_large_a5mR_25c0000419eb2d0c.jpg
  19. 增强Spring @CacheEvict实现key模糊匹配清除
  20. 洛谷 P3387(缩点后+处理 )

热门文章

  1. 六十六、丑数系列,丑的颠覆我的思想
  2. 七十五、Python | Leetcode哈希表系列
  3. pytorch Tensor操作(二)
  4. tornado环境搭建
  5. 直播 | 腾讯天衍实验室张子恒:详细解读天衍实验室知识图谱对齐技术
  6. 自动语音识别(ASR)自监督方法研究综述
  7. Bengio等人新作:基于双层规划的端到端分子构象生成框架
  8. 国内免费GPU资源哪里找,让我告诉你最新的薅羊毛“秘籍”
  9. 不用L约束又不会梯度消失的GAN,了解一下?
  10. 活动 | INTERFACE#4 解读搜狗机器翻译技术,体验搜狗旅行翻译宝产品