欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

iOS 调用地图外的应用程序 应用程序导航(百度/高德/腾讯/系统自带地图) URL API 方法

最编程 2024-03-28 16:59:52
...

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

需求:实现应用外导航。通过选项列表(UIAlertController)的方式提供用户选择,当用户既安装了高德地图和百度地图时,则弹出如下图所示的选项列表。否则用户安装了哪个地图,就增加哪个地图的选择项。根据指定起点、终点以及出行方式,调起腾讯地图APP的路线规划功能,查询出行路线,并在地图中展示。(起点可自动根据定位获取)

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
2.腾讯地图 https://lbs.qq.com/webApi/uriV1/uriGuide/uriMobileRoute

环境配置

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


image.png

判断是否安装地图客户端

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

  • (BOOL)canOpenURL:(NSURL*)url
    步骤 2:判断是否安装了高德/百度地图
    配置完成后,您就可以在自己的app中判断地图客户端是否已安装。
    示例代码如下:
NSURL *scheme = [NSURL URLWithString:@"iosamap://"];
//如果百度地图是 @"baidumap://"  腾讯地图是@"qqmap://"
 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];
    }
}

坐标系转换问题

1.地球坐标 (WGS84)
国际标准,从GPS设备中取出的数据的坐标系
国际地图提供商使用的坐标系
2.火星坐标 (GCJ-02) 也叫国测局坐标系
中国标准,从国行移动设备中定位获取的坐标数据使用这个坐标系
国家规定: 国内出版的各种地图系统(包括电子形式),必须至少采用GCJ-02对地理位置进行首次加密。高德地图、腾讯地图使用
3.百度坐标 (BD-09)
百度标准,百度 SDK,百度地图,Geocoding 使用
百度在火星坐标上进行了二次加密

根据你使用不同地图采用的坐标系,进行相应的坐标系转换处理。不会展示会有偏差。