iOS_URI跳转方式多种地图导航的代码实践
先来看一下我们要达到什么效果,就是当我们点导航的时候,会弹出下面这个选择列表。
当然,如果没有安装某个地图APP,那么对应的选项是不会出现的。检测APP是否安装,只要调用下面这个方法就可以了
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@
"appurlscheme://"
];
关于APP的URL Scheme相关内容这里就不介绍了,大家可以自行去研究
那么我们上图提到了4个地图应用,分别是:
苹果地图
百度地图
高德地图
谷歌地图
这些也是当前我们用得最多的几种地图了(什么,你们说还有腾讯地图? 可惜腾讯地图暂时还不支持URI的方式打开,所以这里就没列出来,等可以用了我会补上)
下面来对比一下几种地图:
地图 | URL Scheme | 文档 | 是否可以跳回到APP |
苹果地图 | 文档 | 否 | |
百度地图 | baidumap:// | 文档 | 否 |
高德地图 | iosamap:// | 文档 | 是 |
谷歌地图 | comgooglemaps:// | 文档 | 是 |
苹果地图是系统自带的(而且苹果地图最好的方式也不是用URI的方式开打),所以无需URL Scheme就可以打开的。
iOS9 开发可能会出现安装了某地图,但是检查不到已经安装!!!
这时候就需要在plist文件配置白名单了
<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string>
<string>iosamap</string>
<string>comgooglemaps</string>
</array>
其次,当跳到地图APP之后可以跳回是一种很好的体验(参考微信的跳转)。但是遗憾的是,苹果地图和百度地图都不支持跳回。
接下来我们就回到正题,说一说每种地图的跳转方式。
假设我们有一个指定的目的坐标coordinate,而我们自己的APP的URL Scheme是urlScheme,名称是appName
假设我们有一个指定的目的坐标coordinate,而我们自己的APP的URL Scheme是urlScheme,名称是appName
1
2
3
|
CLLocationCoordinate2D coordinate; NSString *urlScheme; NSString *appName; |
苹果地图
苹果地图可以通过openURL的方式打开。
详细参数请查看 苹果地图URI API https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html
1
2
|
NSString *urlString = [[NSString stringWithFormat:@ "http://maps.apple.com/?daddr=%f,%f&saddr=slat,slng" ,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; |
但是这种方式,不能以当前位置为起点,所以不符合我们的要求。网上说可以用下面这种方式,但是我没成功
1
|
NSString *urlString = [[NSString stringWithFormat:@ "http://maps.apple.com/?daddr=%f,%f&saddr=Current+Location" ,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
但是苹果提供了另一种方式,使用MKMapItem
1
2
3
4
5
|
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation]; MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]]; [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}]; |
效果如下
百度地图
详细参数请查看 百度地图开放平台> URI API > 接口说明 > iOS端
1
2
|
NSString *urlString = [[NSString stringWithFormat:@ "baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02" ,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; |
要注意几点:
1. origin={{我的位置}}
这个是不能被修改的 不然无法把出发位置设置为当前位置
2. destination=latlng:%f,%f|name=目的地
name=XXXX name这个字段不能省略 否则导航会失败 而后面的文字则可以随便填
3. coord_type=gcj02
coord_type允许的值为bd09ll、gcj02、wgs84 如果你APP的地图SDK用的是百度地图SDK 请填bd09ll 否则 就填gcj02 wgs84你基本是用不上了(关于地图加密这里也不多谈 请自行学习)
效果如下
高德地图
详细参数请查看 高德LBS开放>平台开发 > URI API > iOS
1
2
|
NSString *urlString = [[NSString stringWithFormat:@ "iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2" ,appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; |
要注意几点:
1. sourceApplication=%@&backScheme=%@
sourceApplication代表你自己APP的名称 会在之后跳回的时候显示出来 所以必须填写 backScheme是你APP的URL Scheme 不填是跳不回来的哟
2. dev=0
这里填0就行了,跟上面的gcj02一个意思 1代表wgs84 也用不上
效果如下:
退出导航后,会提示是否跳回到APP
谷歌地图
详细参数请查看官方接口说明:
https://developers.google.com/maps/documentation/ios/urlscheme
1
2
|
NSString *urlString = [[NSString stringWithFormat:@ "comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving" ,appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; |
要注意几点
1. x-source=%@&x-success=%@
跟高德一样 这里分别代表APP的名称和URL Scheme
2. saddr=
这里留空则表示从当前位置触发
效果如下。在有多条路线的时候,谷歌地图会让你选择其中一条
选择之后就进入了导航页面。
腾讯地图
既然提到了腾讯地图 那么还是说一下 从网上和官方文档可以得知 大概调用的URI如下
1
2
|
NSString *urlString = [[NSString stringWithFormat:@ "qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=%f,%f&coord_type=1&policy=0" ,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; |
但是很遗憾,调用之后出错了,无法导航
效果如下
部分实现代码如下:
__block NSString *urlScheme = self.urlScheme;
__block NSString *appName = self.appName;
__block CLLocationCoordinate2D coordinate = self.coordinate;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择地图" 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]];
currentLocation.name = @"我的位置";
toLocation.name = @"目的地位置";
[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 *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alert addAction:action];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
{
UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alert addAction:action];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])
{
UIAlertAction *action = [UIAlertAction actionWithTitle:@"谷歌地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}];
[alert addAction:action];
}
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{
}];
文中的demo可以在这里找到
iOS_URI跳转方式多种地图导航的代码实践的更多相关文章
- URI跳转方式地图导航的代码实践
本文转载至 http://adad184.com/2015/08/11/practice-in-mapview-navigation-with-URI/ 前言 之前介绍了我正在做的是一款定位主打的应用 ...
- ionic3 应用内打开第三方地图导航 百度 高德
1.安装检测第三方APP是否存在的插件 cordova plugin add cordova-plugin-appavailability --save npm install --save @ion ...
- Android studio 百度地图开发(3)地图导航
Android studio 百度地图开发(3)地图导航 email:chentravelling@163.com 开发环境:win7 64位,Android Studio,请注意是Android S ...
- 【krpano】高德地图导航插件(源码+介绍+预览)
简介 krpano可以利用js调用第三方网页版地图,因此可以实现导航效果,用来帮助用户导航到我们全景所在的位置. 效果截图如下,在手机端点击左侧按钮,便会对用户进行定位,跳转至高德地图进行导航 ...
- iOS开发之百度地图导航
本篇主要讲述百度地图的导航功能: 第一步:在使用百度导航之前,我们需要在百度地图开放平台上下载导航的 SDK,共85.8M,网速不好的同学可提前准备好. 第二步:引入导航所需的系统包 将AudioTo ...
- Windows phone 8 学习笔记(8) 定位地图导航(转)
Windows phone 8 已经不使用自家的bing地图,新地图控件可以指定制图模式.视图等.bing地图的定位误差比较大,在模拟器中测试新地图貌似比较理想.本节主要讲解下位置服务以及新地图控件的 ...
- 实现百度地图导航Demo的语音播报功能
上文中实现了在本地导入百度地图导航Demo,那么在此基础上如何实现导航的语音播报呢? 一.为该应用申请语音播报(也叫注册) http://developer.baidu.com/map/index.p ...
- Windows phone 8 学习笔记(8) 定位地图导航
原文:Windows phone 8 学习笔记(8) 定位地图导航 Windows phone 8 已经不使用自家的bing地图,新地图控件可以指定制图模式.视图等.bing地图的定位误差比较大,在模 ...
- 支付宝小程序室内地图导航开发-支付宝小程序JS加载esmap地图
如果是微信小程序开发,请参考微信小程序室内地图导航开发-微信小程序JS加载esmap地图文章 一.在支付宝小程序里显示室内三维地图 需要满足的两个条件 调用ESMap室内地图需要用到小程序web-vi ...
随机推荐
- UI设计教程分享:Ps合成炫酷机械姬
本次给大家分享一个通过PS合成一个炫酷的机械姬,在这个教程里给大家展示图像的色彩处理.人物光影塑造和创意实现及细节处理,教程比较简单,创意十足,看过<机械姬>电影的同学们一定知道这个有多炫 ...
- jvm相关知识点
1.hotspot虚拟机结构:类加载器.堆.栈.方法区.垃圾回收系统.执行引擎.本地方法栈.pc寄存器. 类加载器:负责将class文件从文件系统加载到方法区. 堆:存放对象的一块区域,所有线程共用. ...
- java 开发微信中回调验证一直提示 解密失败处理(Java)
微信公众号平台接入JDK6和JDK7及JDK8加解密失败处理(Java) 根据自己jdk版本编译,如jdk7或者jdk6 ,此时部署后提示报错:java.security.InvalidKeyExce ...
- java面试感悟【一】
我最终选择不包装工作经验,或许是因为我怂,或许是因为一些莫名其妙的坚持…… 然而结果就是在boss上沟通了20多家,只有7家让我投了简历,1家跟我说要我发个时间段给他稍后告诉我面试时间,然后就没有然后 ...
- SQL 创建联合主键Table
CREATE TABLE [User_Instance]( [IntanceID] [int] NOT NULL, ) NOT NULL ) ON [PRIMARY] GO SET ANSI_PADD ...
- spring学习七 spring和dynamic project进行整合
spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开. 注意:为了页面可以访问到servlet,因此servlet必须放进t ...
- 449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...
- Python之内置函数一
一:绝对值,abs i = abs(-123) print(i) # 打印结果 123 二:判断真假,all,与any 对于all # 每个元素都为真,才是True # 假,0,None," ...
- 2018.11.16 bzoj4827: [Hnoi2017]礼物(ntt)
传送门 nttnttntt 入门题. 考虑展开要求的式子∑i=0n−1(xi−yi−c)2\sum_{i=0}^{n-1}(x_i-y_i-c)^2∑i=0n−1(xi−yi−c)2 => ...
- 1-10假期训练(hdu-2059 简单dp)
题目一:传送门 思路:水题,模拟即可 题目二:传送门 思路:dp,决策每个充电站是否要充电.(决策只有搜索,DP两种解决方法) (1)考虑状态的个数,n+2个,因为除了n个还有位置0,终点len两种状 ...