iOS应用内调用第三方地图进行导航

1、配置相对于地图app的LSApplicationQueriesSchemes白名单

在这里插入图片描述

  • 百度地图:baidumap
  • 高德地图:iosamap
  • 腾讯地图:qqmap

2、判断是否能打开第三方地图

这个方法能判断是否能打开相对应的地图
例如想调起百度地图、高德地图或者苹果地图,那么就需要先判断是否能打开(如果你在第一步配置白名单时没填错,那么不能打开就说明手机没有安装相关应用),如果不能打开就提示该用户先去下载该应用

//判断是否能打开百度地图
    NSURL * baidu_App = [NSURL URLWithString:@"baidumap://"];
    if ([[UIApplication sharedApplication] canOpenURL:baidu_App]) {
    
    
        
     }

    //判断是否能打开高德地图
    NSURL * gaode_App = [NSURL URLWithString:@"iosamap://"];
    if ([[UIApplication sharedApplication] canOpenURL:gaode_App]) {
    
    

    }

    //判断是否能打开苹果地图
    NSURL * apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
    if ([[UIApplication sharedApplication] canOpenURL:apple_App]) {
    
    
       
    }

   //判断是否能打开腾讯地图
    NSURL * tencent_App = [NSURL URLWithString:@"qqmap://"];
    if ([[UIApplication sharedApplication] canOpenURL:tencent_App]) {
    
    
        
    }

3、传经纬度调起地图导航

  • 百度地图
    origin:latlng起点纬度,经度 name起点名称
    destination:latlng终点纬度,经度 name终点名称
    mode:出行方式 driving驾车模式
NSString *map = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:39.69,118.15|name:我的位置&destination=latlng:39.92,116.40|name:终点&mode=driving"]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:map] options:@{
    
    } completionHandler:nil];
  • 高德地图
    sourceApplication:第三方调用名称
    sid:起点po id
    slat:起点纬度
    slon:起点经度
    sname:起点名称
    did:终点po id
    dlat:终点纬度
    dlon:终点精度
    dname:终点名称
    dev:起终点是否偏移
    t:0:驾车 1:公交 2:步行
NSString *map =[[NSString stringWithFormat:@"iosamap://path?sourceApplication=demo&sid=&slat=39.69&slon=118.15&sname=我的位置&did=&dlat=39.92&dlon=116.40&dname=终点&dev=0&t=0"]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:map] options:@{
    
    } completionHandler:nil];
  • 腾讯地图
    type:bus公交,drive驾车,walk步行,bike骑行
    from:起点名称
    fromcoord:起点坐标(纬度在前,经度在后,逗号分隔)
    to:终点名称
    tocoord:终点坐标
    referer:开发者key
 NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&fromcoord=39.69,118.15&to=终点&tocoord=39.92,116.40&referer="]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
 [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString] options:@{
    
    } completionHandler:nil];
  • 苹果自带地图
    currentLocation:起点坐标(纬度在前,经度在后)
    tolocation:终点坐标(纬度在前,经度在后)
 CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(39.92,116.40);
 MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(39.69,118.15) addressDictionary:nil]];
 MKMapItem *tolocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
tolocation.name = @"终点";
[MKMapItem openMapsWithItems:@[currentLocation,tolocation]launchOptions:@{
    
    MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];