调用第三方app导航

NSArray * allMapCategory = @[@"百度地图", @"高德地图",@"苹果地图"];

NSMutableArray * mapCategory = [[NSMutableArray alloc] initWithArray:allMapCategory];

NSURL *gaodeURL = [NSURL URLWithString:@"grammermao://grammermao.xhb.com"];

NSURL *baiduURL = [NSURL URLWithString:@"baidumapsdk://mapsdk.baidu.com"];

//baidumapsdk://mapsdk.baidu.com

BOOL gaode = [[UIApplication sharedApplication] canOpenURL:gaodeURL];

BOOL baidu = [[UIApplication sharedApplication] canOpenURL:baiduURL];

if (!gaode) {

          [mapCategory removeObject:@"高德地图"];

}

if (!baidu) {

         [mapCategory removeObject:@"百度地图"];

}

UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

[self.navigationController presentViewController:alert animated:NO completion:nil];

for (int i = 0; i < mapCategory.count; i++) {

UIAlertAction * action = [UIAlertAction actionWithTitle:mapCategory[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//

if ([mapCategory[i] isEqualToString:@"百度地图"]) {

NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=bd09ll",22.643, 114.048, @"东环二路水斗富豪新村"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];

}

if ([mapCategory[i] isEqualToString:@"高德地图"]) {

CLLocationCoordinate2D coord = [self hhTrans_GCGPS:CLLocationCoordinate2DMake(22.643, 114.048)];

NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=0&style=2",@"app name", @"app scheme", @"东环二路水斗富豪新村",coord.latitude, coord.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];

}

if ([mapCategory[i] isEqualToString:@"苹果地图"]) {

MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(22.643, 114.05) addressDictionary:nil];

MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:placemark];

//                toLocation.name = @"to name";

[MKMapItem openMapsWithItems:@[currentLocation, toLocation]

launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];

}

}];

[alert addAction:action];

}

UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

[alert addAction:cancel];


//百度坐标转换成火星坐标

-(CLLocationCoordinate2D)hhTrans_GCGPS:(CLLocationCoordinate2D)baiduGps

{

const double x_pi = M_PI * 3000.0 / 180.0;

CLLocationCoordinate2D googleGps;

double bd_x=baiduGps.longitude - 0.0065;

double bd_y=baiduGps.latitude - 0.006;

double z = sqrt(bd_x * bd_x + bd_y * bd_y) - 0.00002 * sin(bd_y * x_pi);

double theta = atan2(bd_y, bd_x) - 0.000003 * cos(bd_x * x_pi);

googleGps.longitude = z * cos(theta);

googleGps.latitude = z * sin(theta);

return googleGps;

}

你可能感兴趣的:(调用第三方app导航)