iOS跳转到第三地图导航

作者: iOS_ZG | 来源:发表于2016-10-22 11:56 被阅读189次

    跳转到第三方应用必须先设置Scheme 白名单

    1.打开info.plist 如下图设置

    Snip20161024_1.png

    也可以通过info.plist->open As -> Source Code 设置LSApplicationQueriesSchemes

    <key>LSApplicationQueriesSchemes</key>
    <array>
            <string>baidumap</string> //百度地图
            <string>iosamap</string>//高德地图
            <string>comgooglemaps</string>//谷歌地图
            <string>qqmap</string>//腾讯地图
        </array>
    

    2.获取手机地图应用

    
    #pragma mark -导航方法
    
    + (NSArray*)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)endLocation withAddress:(NSString*)address
    
    {
    
    NSMutableArray*maps = [NSMutableArrayarray];
    
    //苹果地图
    
    NSMutableDictionary*iosMapDic = [NSMutableDictionarydictionary];
    
    iosMapDic[@"title"] =@"苹果地图";
    
    [mapsaddObject:iosMapDic];
    
    //百度地图
    
    if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"baidumap://"]]) {
    
    NSMutableDictionary*baiduMapDic = [NSMutableDictionarydictionary];
    
    baiduMapDic[@"title"] =@"百度地图";
    
    NSString*urlString = [[NSStringstringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=北京&mode=driving&coord_type=gcj02",endLocation.latitude,endLocation.longitude]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]];
    
    baiduMapDic[@"url"] = urlString;
    
    [mapsaddObject:baiduMapDic];
    
    }
    
    //高德地图
    
    if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"iosamap://"]]) {
    
    NSMutableDictionary*gaodeMapDic = [NSMutableDictionarydictionary];
    
    gaodeMapDic[@"title"] =@"高德地图";
    
    NSString*urlString = [[NSStringstringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&poiid=BGVIS&lat=%f&lon=%f&dev=1&style=2",applicationName,applicationScheme,address,endLocation.latitude,endLocation.longitude]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]];
    
    gaodeMapDic[@"url"] = urlString;
    
    [mapsaddObject:gaodeMapDic];
    
    }
    
    //谷歌地图
    
    if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"comgooglemaps://"]]) {
    
    NSMutableDictionary*googleMapDic = [NSMutableDictionarydictionary];
    
    googleMapDic[@"title"] =@"谷歌地图";
    
    NSString*urlString = [[NSStringstringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"导航测试",@"nav123456",endLocation.latitude, endLocation.longitude]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]];
    
    googleMapDic[@"url"] = urlString;
    
    [mapsaddObject:googleMapDic];
    
    }
    
    //腾讯地图
    
    if([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"qqmap://"]]) {
    
    NSMutableDictionary*qqMapDic = [NSMutableDictionarydictionary];
    
    qqMapDic[@"title"] =@"腾讯地图";
    
    NSString*urlString = [[NSStringstringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=终点&coord_type=1&policy=0",endLocation.latitude, endLocation.longitude]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]];
    
    qqMapDic[@"url"] = urlString;
    
    [mapsaddObject:qqMapDic];
    
    }
    
    returnmaps;
    
    }
    
    

    3.使用系统actionSheet 弹出展示

    UIAlertControllerStyleActionSheet];
        for (int i = 0 ;i<maps.count;i++) {
            NSDictionary *dict = maps[i];
            NSString *title = dict[@"title"];
            if (i==0) {
                UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self navAppleMapWith:endLocation];
                }];
                [sheet addAction:action];
            }else{
            NSString *urlString= dict[@"url"];
            UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            }];
            [sheet addAction:action];
    
        }
        }
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [sheet addAction:cancel];
        
        [viewController  presentViewController:sheet animated:YES completion:nil];
    

    效果图如下

    Snip20161024_2.png

    附上github的Demo 你可以直接下载使用 https://github.com/iOScoderZG/navDemo

    相关文章

      网友评论

        本文标题:iOS跳转到第三地图导航

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