注意:本文章下的代码有个别变量未知,所以是不能直接跑通的,我也是转别人的

在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. }
      28. 原文:http://blog.csdn.net/u011011341/article/details/9233049#

iOS开发----调用地图导航的更多相关文章

  1. IOS开发之地图导航

    一.问题描述 现在很多的APP 都开始引入了地图和定位功能,包括一些餐饮业,团购等.他们都过定位和地图来让用户更加方便的根据自己的位置找到合适的目标,也就是说,现在地图定位已经不再是导航工具类,地图工 ...

  2. IOS开发UI篇—导航控制器属性和基本使用

    IOS开发UI篇—导航控制器属性和基本使用 一.导航控制器的一些属性和基本使用 1.把子控制器添加到导航控制器中的四种方法 (1) 1.创建一个导航控制器 UINavigationController ...

  3. iOS 调用地图导航

    在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤 #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevic ...

  4. iOS开发系列--地图与定位

    概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...

  5. 转-iOS开发系列--地图与定位

    来自: http://www.cnblogs.com/kenshincui/p/4125570.html#autoid-3-4-0 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功 ...

  6. iOS开发系列--地图与定位总结

    现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个陌生的 ...

  7. IOS开发中设置导航栏主题

    /** * 系统在第一次使用这个类的时候调用(1个类只会调用一次) */ + (void)initialize { // 设置导航栏主题 UINavigationBar *navBar = [UINa ...

  8. iOS开发中地图开发的简单应用

    iOS上使用地图比Android要方便,只需要新建一个MKMapView,addSubView即可.这次要实现的效果如下: 有标注(大头针),定位,地图. 1.添加地图 1.1 新一个Single V ...

  9. IOS 手绘地图导航

    手绘地图导航 第三方库 NAMapKit, 1)支持在手绘图上标记.缩放 2)支持在单张图片 3)支持瓦片小图片 思路 前提:美工已经切好手绘图,并告知我们当前的缩放级别. 1)确定好手绘图左上角点在 ...

随机推荐

  1. URL验证

    function isURL(str_url) { var strRegex = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0- ...

  2. There is no getter for property named 'NULL' in ……

    往往细节上的错误事最要命的事情,当你看着代码,逻辑上没有问题,但是却又曝出一些莫名其妙不知所以的错,你百度了 说出来的原因又是乱七八糟的鸡肋!很无助,纠结了很久,浪费了很多宝贵的时间--看代码! &l ...

  3. phpexcel相关函数

    1.header [php] header("Content-Type:application/vnd.ms-excel"); header("Content-Dispo ...

  4. iterator 及 迭代器模式(转发)

    Iterator definitions An iterator is any object that, pointing to some element in a range of elements ...

  5. DSS中间件介绍

    DSS中间件采用HTTP协议,终端可以是任何的支持Http协议的设备,开发工具和语言均不受限制 DMS消息服务, 采用类似HTTP的协议 DSS-API介绍(持续更新) http://www.dioc ...

  6. 教你理解Fragment

    定义 Fragment 表示 Activity 中的行为或用户界面部分.我们可以将多个片段组合在一个 Activity 中来构建多窗口UI,以及在多个 Activity 中重复使用某个片段.可以将片段 ...

  7. 10 个强大的开源 Web 流量分析工具(转帖)

    Web 流量分析工具多不胜数,从 WebTrends 这样专业而昂贵的,到 Google Analytics 这样强大而免费的,从需要在服务器端单独部署的,到可以从前端集成的,不一而足.本文收集并介绍 ...

  8. cocos2d-x源码分析(1)

    class CC_DLL CCCopying { public: virtual CCObject* copyWithZone(CCZone* pZone); }; class CC_DLL CCZo ...

  9. mac配置java开发环境: jdk1.7 +sdk1.7+maven +tomcat

    1.先安装jdk ,才能安装sdk .2 mac中jdk1.7的默认位置:/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home ...

  10. CAST 类型转换应用

    1: select 2: ID,SystemID,Department, 3: case Number when 0 then '若干' else CAST(Number as varchar)+'人 ...