首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >iOS_调起各个地图软件

iOS_调起各个地图软件

作者头像
mikimo
发布2022-07-20 13:56:57
发布2022-07-20 13:56:57
43400
代码可运行
举报
文章被收录于专栏:iOS开发~iOS开发~
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
运行
复制
#pragma mark - 弹出选择地图alert
+ (void)popMapsAlertWithVC:(UIViewController *)vc toCoor:(CLLocationCoordinate2D)toCoor targetName:(NSString *)targetName {
  NSArray *mapSchemeArr = @[@"iosamap://", @"baidumap://", @"qqmap://", @"comgooglemaps://"];
  NSArray *mapName = @[@"高德地图", @"百度地图", @"腾讯地图", @"谷歌地图"];
  
  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"导航" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  for (int i = 0; i < mapSchemeArr.count; i++) {
    NSURL *url = [NSURL URLWithString:mapSchemeArr[i]];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
      UIAlertAction *action = [UIAlertAction actionWithTitle:mapName[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [WWTKTool openMapWith:mapName[i] toCoor:toCoor targetName:targetName];
      }];
      [alert addAction:action];
    }
  }
  UIAlertAction *actionSystem = [UIAlertAction actionWithTitle:@"系统地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [WWTKTool openAppleMapWithToCoor:toCoor targetName:targetName];
  }];
  [alert addAction:actionSystem];
  UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  [alert addAction:cancelAction];
  [vc presentViewController:alert animated:YES completion:nil];
}

#pragma mark - 打开对应的导航app
+ (void)openMapWith:(NSString *)mapName toCoor:(CLLocationCoordinate2D)toCoor targetName:(NSString *)targetName {
  if ([mapName isEqualToString:@"高德地图"]) {
    NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=applicationName&backScheme=com.mobvoi.one&lat=%f&lon=%f&dev=0&style=2", toCoor.latitude, toCoor.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
      [[UIApplication sharedApplication] openURL:url];
    }
  } else if ([mapName isEqualToString:@"百度地图"]) {
    NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=gcj02", toCoor.latitude, toCoor.longitude, targetName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
      [[UIApplication sharedApplication] openURL:url];
    }
  } else if ([mapName isEqualToString:@"腾讯地图"]) {
    NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=%f,%f&coord_type=1&policy=0", toCoor.latitude, toCoor.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
      [[UIApplication sharedApplication] openURL:url];
    }
  } else if ([mapName isEqualToString:@"谷歌地图"]) {
    NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?daddr=%@&sll=%.8f,%.8f&directionsmode=transit", targetName, toCoor.latitude, toCoor.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
      [[UIApplication sharedApplication] openURL:url];
    }
  }
}

#pragma mark - 打开苹果地图
+ (void)openAppleMapWithToCoor:(CLLocationCoordinate2D)toCoor targetName:(NSString *)targetName {
  MKMapItem *location = [MKMapItem mapItemForCurrentLocation];
  CLLocationCoordinate2D gc02Coor = [TQLocationConverter transformFromBaiduToGCJ:toCoor];
  MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:gc02Coor addressDictionary:nil];
  MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:placemark];
  toLocation.name = targetName;
  NSArray *items = [NSArray arrayWithObjects:location, toLocation, nil];
  NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeWalking,
                            MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),
                            MKLaunchOptionsShowsTrafficKey:@YES}; //打开苹果自身地图应用,并呈现特定的item
  [MKMapItem openMapsWithItems:items launchOptions:options];
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-08-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档