美文网首页iOS常用iOS 宝典
iOS 调起应用外地图App导航(百度/高德/系统自带地图)UR

iOS 调起应用外地图App导航(百度/高德/系统自带地图)UR

作者: 永永不止步 | 来源:发表于2020-07-17 12:51 被阅读0次

在地图类应用开发中,我们经常有导航这个功能需求。根据导航方式可以分为应用内导航和应用外导航,其中应用内导航指的是使用第三方提供的地图SDK(高德、百度等)嵌入到我们开发的APP内部。应用外导航指的是以URL Scheme 跳转的方式,跳转到对应的地图APP中,使用对方的导航功能。
不使用 SDK 进行开发,而是直接调用 (百度,高德,系统自带高德)APP。这样给了客户多重选择。更减少了引入 SDK 使 APP 臃肿的问题。适用于移动设备浏览器端应用和移动App应用均可调起iOS版移动应用地图客户端

需求:实现应用外导航。通过选项列表(UIAlertController)的方式提供用户选择,当用户既安装了高德地图和百度地图时,则弹出如下图所示的选项列表。否则用户安装了哪个地图,就增加哪个地图的选择项。

image.png

URL 开发文档地址

1.百度地图 https://lbsyun.baidu.com/index.php?title=uri/api/ios
2.高德地图 https://lbs.amap.com/api/amap-mobile/guide/ios/ios-uri-information

环境配置

配置白名单 由于iOS的限制,iOS9之后app想调起高德地图/百度地图,必须在自己app设置中配置白名单
配置方法:
1、找到您的Info.plist文件
2、在文件中添加key:LSApplicationQueriesSchemes,类型是Array,如果曾经添加过,无需再次添加。
3、Array中添加一个item,类型为String,值为高德:iosamap / 百度:baidumap。 如图所示


image.png

判断是否安装地图客户端

步骤 1:通过查看是否可以打开该 scheme, 判断是否安装高德/百度地图

  • (BOOL)canOpenURL:(NSURL*)url
    步骤 2:判断是否安装了高德/百度地图
    配置完成后,您就可以在自己的app中判断高德地图是否已安装。
    示例代码如下:
NSURL *scheme = [NSURL URLWithString:@"iosamap://"];
//如果百度地图是 @"baidumap://"
 BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:scheme];

如果canOpen为YES,则安装了高德/百度地图;如果canOpen为NO,则未安装高德地图。
注意:苹果自带地图不需要检测,默认已经安装

使用

#pragma mark - 调取导航
/// 导航功能方法封装
/// @param lat 纬度
/// @param lng 经度
/// @param address 地图显示目的地
/// @param currentController 在哪个VC弹出
- (void)mapNavigationLat:(double)lat
                     lng:(double)lng
                 address:(NSString *)address
       currentController:(UIViewController *)currentController{
    __block NSString *urlScheme = @"vlife://";//设置本APPurlScheme 可修改
    __block NSString *appName = @"vlife";//设置APP名称 可修改
   //设置目的地的经纬度结构体
    __block CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(lat,lng);
    if (address == nil || address.length == 0) {
        address = @"目的地";
    }
    UIAlertController *alert = [UIAlertController  alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
            MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
            toLocation.name = address;
            [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                           launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
        }];
        [alert addAction:action];
    }
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSString *badiduStr = [NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=name:%@|latlng:%f,%f&mode=driving&coord_type=gcj02",address,coordinate.latitude, coordinate.longitude];
            NSString *urlString = [badiduStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) { NSLog(@"scheme调用结束"); }];
        }];
        [alert addAction:action];
    }
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSString *gaodeStr = [NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&dlat=%f&dlon=%f&dname=%@&dev=0&t=0",appName,urlScheme,coordinate.latitude, coordinate.longitude,address];
            NSURL *myLocationScheme = [NSURL URLWithString:[gaodeStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
            [[UIApplication sharedApplication] openURL:myLocationScheme options:@{} completionHandler:^(BOOL success) { NSLog(@"scheme调用结束"); }];
        }];
        [alert addAction:action];
    }
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    if (currentController) {
        [currentController presentViewController:alert animated:YES completion:nil];
    }
}

相关文章

网友评论

    本文标题:iOS 调起应用外地图App导航(百度/高德/系统自带地图)UR

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