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

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

  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://"]])

参考

示例代码

  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

  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

    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. iOS开发----调用地图导航

    注意:本文章下的代码有个别变量未知,所以是不能直接跑通的,我也是转别人的 在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤 #define SYSTEM_VERSION_L ...

  2. ios调用系统导航

    #import "ViewController.h" #import <MapKit/MapKit.h> @interface ViewController () @p ...

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

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

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

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

  5. iOS原生地图开发进阶——使用导航和附近兴趣点检索

    iOS原生地图开发进阶——使用导航和附近兴趣点检索 iOS中的mapKit框架对国际化的支持非常出色.在前些篇博客中,对这个地图框架的基础用法和标注与覆盖物的添加进行了详细的介绍,这篇博客将介绍两个更 ...

  6. iOS 开发笔记 - 导航到地图

    导航到地图,已经不是什么新鲜事了.网上有好多参考的资料,我总结出只需要两步 第一步:在info中加上支持的各平台 比如:iosamap高德地图.comgooglemaps谷歌地图.baidumap百度 ...

  7. iOS调用第三方地图App进行导航方法

    前言 App内根据手机上装载的地图App将其显示在弹出的选择框,选择对应地图跳转进入地图导航.需要用到- (BOOL)canOpenURL:(NSURL *)url NS_AVAILABLE_IOS( ...

  8. H5调用百度地图导航

    template <div class="map"> <div class="content_flex"><img src=&qu ...

  9. iOS开发之百度地图导航

    本篇主要讲述百度地图的导航功能: 第一步:在使用百度导航之前,我们需要在百度地图开放平台上下载导航的 SDK,共85.8M,网速不好的同学可提前准备好. 第二步:引入导航所需的系统包 将AudioTo ...

随机推荐

  1. WCF学习笔记

    1,关于WCF/web service/WSE Web Service:是行业标准,也就是Web Service 规范,也称作WS-*规范,既不是框架,也不是技术.它有一套完成的规范体系标准,而且在持 ...

  2. java作业——整数相加

    设计思路:由于命令行参数都是字符串,所以解决问题的关键在于字符串和整数之间的转化.首先定义数组,让所要相加的数组成一个数组,然后实现数组的字符串转化为整数,最后相加输出就行了. 程序流程图: 源代码: ...

  3. BZOJ1595 [Usaco2008 Jan]人工湖

    直接模拟...从最低的开始向两边拓展= = /************************************************************** Problem: 1595 ...

  4. 快速将excel数据保存到Oracle数据库中【转】

    我们在工作中,也许会碰到以下情况,客户或者同事发来需要调查的数据,并不是dmp文件,而是excel文件,此时通常是一张表,少量几条记录.最近我恰好碰到了这种情况,所以做了些调查,不敢藏私,拿出来跟大家 ...

  5. oracle常见权限分配

    1.GRANT 赋于权限 常用的系统权限集合有以下三个: CONNECT(基本的连接), RESOURCE(程序开发), DBA(数据库管理) 常用的数据对象权限有以下五个: ALL ON 数据对象名 ...

  6. PMP项目管理笔记 项目定义

    项目的定义 项目是为创造独特的产品,服务或成果而进行临时性的工作. 项目是组织的经营需要与战略目标服务的. PMBOK 指南描述的项目管理知识,从本质上讲,是用来管理中等或以上规模,跨部门,跨专业的目 ...

  7. PHP分页类库

    <?php /** * @title: Ekcms page分页类库 * @version: 1.0 * @author: perry <perry@1kyou.com> * @pu ...

  8. asp.net下ajax.ajaxMethod使用方法

    使用AjaxMethod可以在客户端异步调用服务端方法,简单地说就是在JS里调用后台.cs文件里的方法,做一些JS无法做到的操作,如查询数据库.   使用AjaxMethod要满足一下几点: 1.如果 ...

  9. 操作系统cmd算法

    实验一  命令解释程序的编写(两周内) 一.目的和要求 1. 实验目的 (1)掌握命令解释程序的原理: (2)*掌握简单的DOS调用方法: (3)掌握C语言编程初步. 2.实验要求 编写类似于DOS, ...

  10. 封装自己的JS库

    一.基础知识 1.点击计数 第一种: var aBtn=document.getElementsByTagName('input'); var i=0; for(i=0;i<aBtn.lengt ...