IOS 跳转到百度地图,高的地图,谷歌地图进行导航

//跳到地图导航
- (void)btnPermanentAddressClick
{
    NSString *appName = @"自己的app名字";
    NSString *urlScheme = @"自己的urlScheme";
    UIAlertController * alert =  [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * appleMap = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
        if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://"]])
        {
            //地理编码器
            CLGeocoder *geocoder = [[CLGeocoder alloc] init];
            [geocoder geocodeAddressString:@"上海嘉定区伊宁路2000号" completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
                for (CLPlacemark *placemark in placemarks) {
                    //坐标(经纬度)
                    CLLocationCoordinate2D coordinate = placemark.location.coordinate;
                    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
                    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
                    [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
                                                   MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];

                }
            }];
            
        }else{
            [NXPopAlertViewLabel showMessage:@"您的手机没有安装苹果地图"];
        }
    }];
    
    UIAlertAction * baiduMap = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
        if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://"]])
        {
            //地理编码器
            CLGeocoder *geocoder = [[CLGeocoder alloc] init];
            [geocoder geocodeAddressString:@"上海嘉定区伊宁路2000号" completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
                for (CLPlacemark *placemark in placemarks) {
                    //坐标(经纬度)
                    CLLocationCoordinate2D coordinate = placemark.location.coordinate;
                    NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
                }
            }];
        }else{
            [NXPopAlertViewLabel showMessage:@"您的手机没有安装百度地图"];
        }
    }];
    UIAlertAction * gaodeMap = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
        
        if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]])
        {
            //地理编码器
            CLGeocoder *geocoder = [[CLGeocoder alloc] init];
            [geocoder geocodeAddressString:@"上海嘉定区伊宁路2000号" completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
                for (CLPlacemark *placemark in placemarks) {
                    //坐标(经纬度)
                    CLLocationCoordinate2D coordinate = placemark.location.coordinate;
                    NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
                }
            }];
        }else{
            [NXPopAlertViewLabel showMessage:@"您的手机没有安装高德地图"];
        }
    }];
    UIAlertAction * gugeMap = [UIAlertAction actionWithTitle:@"谷歌地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
        
        if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])
        {
            //地理编码器
            CLGeocoder *geocoder = [[CLGeocoder alloc] init];
            [geocoder geocodeAddressString:@"上海嘉定区伊宁路2000号" completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
                for (CLPlacemark *placemark in placemarks) {
                    //坐标(经纬度)
                    CLLocationCoordinate2D coordinate = placemark.location.coordinate;
                    NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
                }
            }];
        }else{
            [NXPopAlertViewLabel showMessage:@"您的手机没有安装谷歌地图"];
        }
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
        
    }];
    [alert addAction:appleMap];
    [alert addAction:baiduMap];
    [alert addAction:gaodeMap];
    [alert addAction:gugeMap];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

你可能感兴趣的:(IOS 跳转到百度地图,高的地图,谷歌地图进行导航)