美文网首页
拉起本地地图导航

拉起本地地图导航

作者: MrLiZ | 来源:发表于2017-03-25 21:49 被阅读0次

    当然 如果没有安装某个地图APP 那么对应的选项是不会出现的 检测APP是否安装 只要调用下面这个方法就可以了

    [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"appurlscheme://"]

    关于APP的URL Scheme相关内容这里就不介绍了 大家可以自行去研究

    那么我们上图提到了4个地图应用 分别是

    苹果地图

    百度地图

    高德地图

    谷歌地图

    这些也是当前我们用得最多的几种地图了(什么 你们说还有腾讯地图? 可惜腾讯地图暂时还不支持URI的方式打开 所以这里就没列出来 等可以用了我会补上)

    下面来对比一下几种地图

    地图URL Scheme文档是否可以跳回到APP

    苹果地图文档

    百度地图baidumap://文档

    高德地图iosamap://文档

    谷歌地图comgooglemaps://文档

    苹果地图是系统自带的(而且苹果地图最好的方式也不是用URI的方式开打) 所以无需URL Scheme就可以打开的

    其次 当跳到地图APP之后可以跳回是一种很好的体验(参考微信的跳转) 但是遗憾的是 苹果地图和百度地图都不支持跳回

    接下来我们就回到正题 说一说每种地图的跳转方式

    假设我们有一个指定的目的坐标coordinate而我们自己的APP的URL Scheme是urlScheme名称是appName

    CLLocationCoordinate2D coordinate;NSString *urlScheme;NSString *appName;

    苹果地图

    苹果地图可以通过openURL的方式打开

    NSString*urlString = [[NSStringstringWithFormat:@"http://maps.apple.com/?daddr=%f,%f&saddr=slat,slng",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding][[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];

    但是这种方式不能以当前位置为起点所以不符合我们的要求 网上说可以用下面这种方式 但是我没成功

    NSString*urlString = [[NSStringstringWithFormat:@"http://maps.apple.com/?daddr=%f,%f&saddr=Current+Location",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    但是苹果提供了另一种方式 使用MKMapItem

    MKMapItem*currentLocation = [MKMapItemmapItemForCurrentLocation];MKMapItem*toLocation = [[MKMapItemalloc] initWithPlacemark:[[MKPlacemarkalloc] initWithCoordinate:coordinate addressDictionary:nil]];[MKMapItemopenMapsWithItems:@[currentLocation, toLocation]                launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumbernumberWithBool:YES]}];

    效果如下

    百度地图

    NSString*urlString = [[NSStringstringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];

    要注意几点

    origin={{我的位置}}

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

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

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

    coord_type=gcj02

    coord_type允许的值为bd09ll、gcj02、wgs84 如果你APP的地图SDK用的是百度地图SDK 请填bd09ll 否则 就填gcj02 wgs84你基本是用不上了(关于地图加密这里也不多谈 请自行学习)

    效果如下

    高德地图

    NSString*urlString = [[NSStringstringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];

    要注意几点

    sourceApplication=%@&backScheme=%@

    sourceApplication代表你自己APP的名称 会在之后跳回的时候显示出来 所以必须填写  backScheme是你APP的URL Scheme  不填是跳不回来的哟

    dev=0

    这里填0就行了 跟上面的gcj02一个意思  1代表wgs84 也用不上

    效果如下

    退出导航后 会提示是否跳回到APP

    谷歌地图

    NSString*urlString = [[NSStringstringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];

    要注意几点

    x-source=%@&x-success=%@

    跟高德一样 这里分别代表APP的名称和URL Scheme

    saddr=

    这里留空则表示从当前位置触发

    效果如下 在有多条路线的时候 谷歌地图会让你选择其中一条

    选择之后就进入了导航页面

    腾讯地图

    既然提到了腾讯地图 那么还是说一下 从网上和官方文档可以得知 大概调用的URI如下

    NSString*urlString = [[NSStringstringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=%f,%f&coord_type=1&policy=0",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];

    相关文章

      网友评论

          本文标题:拉起本地地图导航

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