美文网首页
iOS 跳转高德,百度,腾讯,谷歌,苹果地图导航

iOS 跳转高德,百度,腾讯,谷歌,苹果地图导航

作者: AidaHe | 来源:发表于2019-06-10 09:27 被阅读0次

    一、info.plist文件添加Schemes

    <key>LSApplicationQueriesSchemes</key>
        <array>
            <string>iosamap</string>
            <string>baidumap</string>
            <string>comgooglemaps</string>
            <string>qqmap</string>
        </array>
    

    二、获取当前经纬度和地址

    三、开始导航

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请选择导航" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        NSURL * apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
        float latitude = [_model.latitude floatValue];
        float longitude = [_model.longitude floatValue];
        float locLaitude = self.localLatitude;
        float locLongitude = self.localLongitude;
        NSString *locAddress = self.localAddress;
        NSString *address = _model.address;
        UIAlertAction *appleAction = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if ([[UIApplication sharedApplication] canOpenURL:apple_App]) {
                NSString *urlString=[NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",locLaitude ,locLongitude,latitude,longitude ];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            }else{
                [SVProgressHUD showSuccessWithStatus:@"请先安装苹果地图客户端"];
            }
        }];
        
        NSURL * gaode_App = [NSURL URLWithString:@"iosamap://"];
        UIAlertAction *gaodeAction = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if ([[UIApplication sharedApplication] canOpenURL:gaode_App]) {
                NSString *urlString=[[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&slat=%f&slon=%f&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&t=0",locAddress,locLaitude,locLongitude,locAddress,latitude,longitude,address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            }else{
                [SVProgressHUD showSuccessWithStatus:@"请先安装高德地图客户端"];
            }
        }];
        
        NSURL * baidu_App = [NSURL URLWithString:@"baidumap://"];
        UIAlertAction *baiduAction = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if ([[UIApplication sharedApplication] canOpenURL:baidu_App]) {
                NSString *urlString=[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",latitude,longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            }else{
                [SVProgressHUD showSuccessWithStatus:@"请先安装百度地图客户端"];
            }
        }];
        
        NSURL * tencent_App = [NSURL URLWithString:@"qqmap://"];
        UIAlertAction *tencentAction = [UIAlertAction actionWithTitle:@"腾讯地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if ([[UIApplication sharedApplication] canOpenURL:tencent_App]) {
                NSString *urlString=[[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=%f,%f&tocoord=%f,%f&policy=1",locLaitude,locLongitude,latitude,longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            }else{
                [SVProgressHUD showSuccessWithStatus:@"请先安装腾讯地图客户端"];
            }
        }];
        
        NSURL * google_App = [NSURL URLWithString:@"comgooglemaps://"];
        UIAlertAction *googleAction = [UIAlertAction actionWithTitle:@"谷歌地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if ([[UIApplication sharedApplication] canOpenURL:google_App]) {
                NSString *urlString=[[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"***",@"ChuanLaoDa",latitude,locLongitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
                
            }else{
                [SVProgressHUD showSuccessWithStatus:@"请先安装谷歌地图客户端"];
            }
        }];
        
        [alertVC addAction:cancelAction];
        [alertVC addAction:appleAction];
        [alertVC addAction:gaodeAction];
        [alertVC addAction:baiduAction];
        [alertVC addAction:tencentAction];
        [alertVC addAction:googleAction];
        [self presentViewController:alertVC animated:false completion:nil];
    

    相关文章

      网友评论

          本文标题:iOS 跳转高德,百度,腾讯,谷歌,苹果地图导航

          本文链接:https://www.haomeiwen.com/subject/gdnrxctx.html