美文网首页
iOS Mapkit URL跳转导航

iOS Mapkit URL跳转导航

作者: 乡道狗毛草 | 来源:发表于2015-12-17 13:39 被阅读690次

    自己做地图应用导航没有特殊要求现在一般都是用URL跳转手机内地图app导航,这样做的好处有很多,不多介绍,看看主流软件的地图小功能导航都是URL跳转导航!现在就介绍下跳转导航的,做法是以URI跳转的方式(在iOS中就是以URL Scheme的方式),直接跳到对应的地图APP中,直接利用对方的功能来导航。

    看看效果

    Paste_Image.png Paste_Image.png

    这里是各个地图的官方文档和提供的URL Scheme在开始之前你有可能需要去了解一下

    地图 URL Scheme 文档
    苹果地图 文档
    百度地图 baidumap:// 文档
    高德地图 iosamap:// 文档
    google地图 comgooglemaps:// 文档

    首先必须需要一个地点即坐标位置,一般大头针都会有这个属性

     @property (nonatomic) CLLocationCoordinate2D coordinate;
    

    首先设置UIAlertController 和取消按钮 注意UIAlertController在低于iOS8 版本中会崩

    • 设置取消按钮

      NSString *title =[NSString stringWithFormat:@"导航到设备"];
      UIAlertController *alerController =[UIAlertController alertControllerWithTitle:title        message:nil preferredStyle:UIAlertControllerStyleActionSheet];
      UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
      
    • 设置高德地图按钮

      UIAlertAction  *gaodeMapAction=[UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      NSString *urlsting =[[NSString stringWithFormat:@"iosamap://navi?sourceApplication= &backScheme= &lat=%f&lon=%f&dev=0&style=2",self.coordinate.latitude,self.coordinate.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication  sharedApplication]openURL:[NSURL URLWithString:urlsting]]; }];
      

    URL中有个dev=0,0代表传入的地图坐标为gcj02(这个是中国加密后的坐标值) 填写1 就代表坐标类型为wgs84 (原始坐标,国际通用)关于火星坐标和地球坐标大家有必要去了解下不然很坑
    style= 表示的是导航类型,比如步行啊 公交车啊 还是自己驾车类型,可以去官方文档了解,不多介绍,如果需要自己驾车导航就填2吧。

    • 设置 百度导航按钮

       UIAlertAction *baiduMapAction=[UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       NSString *urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",self.coordinate.latitude,self.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]]; }];
      

    1.origin={{我的位置}}

    这个是不能被修改的 不然无法把出发位置设置为当前位置

    2.destination=latlng:%f,%f|name=目的地

    name=XXXX name这个字段不能省略 否则导航会失败 而后面的文字则可以随便填

    3.coord_type=gcj02

    coord_type允许的值为bd09ll、gcj02、wgs84 和上面的dev=类似,这里多了一个bd09ll 这个坐标是百度自己加密过一次,要进行坐标转换的,自己google学习下。

    • 设置google地图按钮
      UIAlertAction *googleMapAction=[UIAlertAction actionWithTitle:@"Google地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      NSString *urlsting =[[NSString stringWithFormat:@"comgooglemaps://?x-source= &x-success= &saddr=&daddr=%f,%f&directionsmode=driving",self.coordinate.latitude,self.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];
      }];

    这里不多讲了差不多一个套路
    注意这题就差不多了
    saddr=这里留空则表示从当前位置触发 在有多条路线的时候,谷歌地图会让你选择其中一条

    • 最后是自带地图导航 和之前的有点不一样 用URL貌似行不通 但是苹果提供了另一种方式,使用MKMapItem

        UIAlertAction *ownMapAction=[UIAlertAction actionWithTitle:@"自带地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        MKMapItem *currentLocation =[MKMapItem mapItemForCurrentLocation];
      
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:self.coordinate addressDictionary:nil]];
      
        [MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
                                                                                   MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
      
        }];
      

    最后判断手机app是否有相应的软件,如果有就添加到弹框,如果没有就相应的不添加

        [alerController addAction:cancelAction];//添加取消按钮
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]) {
        [alerController addAction:baiduMapAction];
    }
    if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        [alerController addAction:gaodeMapAction ];
    };
    if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
        [alerController addAction:googleMapAction ];
    }
    [alerController addAction:ownMapAction];
    

    这里的[UIApplication sharedApplication] canOpenURL:NSURL]会返回一个BOOL值,yes代表有相应的app

    最后防止崩溃我写了一个笨方法

    if (objc_getClass("UIAlertController") !=nil) {
        [self presentViewController:alerController animated:YES completion:nil];
    
    }else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"请升级系统版本后用此功能" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] ;
        [alert show];
    }
    

    判断能不能生成UIAlertController!

    好了 就介绍到这里了
    最后要注意的是新版xcode7 新增的特性

    直接复制~
    1.在iOS9中,如果没有添加上述白名单,系统会打印类似如下提示:
    .-canOpenURL: failed for URL: “sinaweibohdsso://xxx” – error: “This app is not allowed to query for scheme sinaweibohdsso”(如下图)
    如没有添加相关白名单,有可能导致分享失败,例如不会跳转微信,不会跳转****QQ****等

    2.添加完上述所需的名单,系统依然会打印类似信息:
    .-canOpenURL: failed for URL: “sinaweibohdsso://xxx” – error: “null”
    这是系统打印的信息,目前是无法阻止其打印,即无法消除的

    Paste_Image.png

    上连接 具体自己看看吧
    http://wiki.mob.com/ios9-%E5%AF%B9sharesdk%E7%9A%84%E5%BD%B1%E5%93%8D%EF%BC%88%E9%80%82%E9%85%8Dios-9%E5%BF%85%E8%AF%BB%EF%BC%89/

    相关文章

      网友评论

          本文标题:iOS Mapkit URL跳转导航

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