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

在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#

最新文章

  1. swift 的枚举、结构体、类
  2. JAVA中static关键字
  3. Html中metra的含义
  4. sqlldr导入数据
  5. (easy)LeetCode 225.Implement Stack using Queues
  6. LightOj 1096 - nth Term (矩阵快速幂,简单)
  7. sharepoint Linq方式的增,删,查,改
  8. [转:CSS3-前端] CSS3发光和多种图片处理
  9. c3p0链接池
  10. public &lt;T&gt; void method(T var)
  11. PHP + Memcache 实现Session共享
  12. Django 1.11 release note简明解读
  13. Android 开发 音视频从入门到提高 任务列表 转载
  14. Lua + Redis 解决高并发
  15. 信用评分卡 (part 1 of 7)
  16. functions 示例
  17. Mybatis中 collection 和 association 的区别?
  18. php插入上万条mysql数据最快的方法
  19. Lambda学习---StreamApi使用
  20. 委托、Lambda表达式、事件系列03,从委托到Lamda表达式

热门文章

  1. MySQL安装常见问题(找不到文件,系统服务无法启动...)
  2. shell scripts
  3. 高频交易策略之Penny Jump[z]
  4. java 开发常用的Linux命令
  5. 清空form表单下所有的input值-------------jquery
  6. 复利test
  7. [更新中]并发和并行(Concurrency and Parallelism)
  8. getline函数的用法
  9. 纯css径向渐变(CSS3--Gradient)
  10. office2003-2007 绿色版 出错 文件丢失(未解决)