iOS_調起各個地圖軟件

#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];
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章