iOS 跳轉高德,百度,騰訊,谷歌,蘋果地圖導航

一、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];
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容