iOS通过URL调用第三方地图进行导航
2016-05-25 00:00
701 查看
在app中有许多场景需要用到导航,比如 我给你发了一个地理位置,你可以打开这个位置然后通过位置导航找到我.
导航可以通过内嵌三方SDK实现也可以通过跳转三方app实现,后者相对前者来说相对简单而且也能有效的减小app体积.下面就为大家介绍下后者实现.
下面实现actionSheet的代理方法
// 通用参数
self.appName = app名称 self.urlScheme = appUrlScheme _toLocationCoordinate = 目的地经纬度
// 只有调用百度地图才需要当前的经纬度
_myLocation = 当前经纬度
具体 这些URL以及参数的设置都可以在官方文档查看
需要注意的是 iOS9.0以上的系统需要在info.plist文件设置白名单才能调起三方地图
白名单设置
![](http://static.oschina.net/uploads/space/2016/0525/172859_zZyL_2604520.png)
设置URLScheme
info->URLTypes 添加urlScheme
如此便可以轻松的在app中调用三方地图进行导航了!
导航可以通过内嵌三方SDK实现也可以通过跳转三方app实现,后者相对前者来说相对简单而且也能有效的减小app体积.下面就为大家介绍下后者实现.
// 代码如下 这里只添加了三种地图 (苹果官方地图,高德,百度),如有其它需要可以自己添加,如果没有安装高德或者百度地图 sheet的titles里面是不会显示的 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择地图" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil, nil]; if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]) { [sheet addButtonWithTitle:@"苹果地图"]; } if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) { [sheet addButtonWithTitle:@"百度地图"]; } if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) { [sheet addButtonWithTitle:@"高德地图"]; } [sheet showInView:self.view];
下面实现actionSheet的代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex]; if ([title isEqualToString:@"苹果地图"]) { MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation]; MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:_currentLocationCoordinate addressDictionary:nil]]; // 目的地名字 toLocation.name = self.addressString; [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}]; } else if ([title isEqualToString:@"高德地图"]) { NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",self.appName,self.urlScheme, _toLocationCoordinate.latitude, _toLocationCoordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; } else if ([title isEqualToString:@"百度地图"]) { NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=latlng:%f,%f|name:%@&mode=driving&src=dangdang://&coord_type=gcj02", _myLocation.latitude,_myLocation.longitude, _toLocationCoordinate.latitude, _toLocationCoordinate.longitude, self.addressString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%@",urlString); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; } }
// 通用参数
self.appName = app名称 self.urlScheme = appUrlScheme _toLocationCoordinate = 目的地经纬度
// 只有调用百度地图才需要当前的经纬度
_myLocation = 当前经纬度
具体 这些URL以及参数的设置都可以在官方文档查看
需要注意的是 iOS9.0以上的系统需要在info.plist文件设置白名单才能调起三方地图
白名单设置
![](http://static.oschina.net/uploads/space/2016/0525/172859_zZyL_2604520.png)
设置URLScheme
info->URLTypes 添加urlScheme
如此便可以轻松的在app中调用三方地图进行导航了!
相关文章推荐
- iOS 高德地图(-)(基础篇)
- iOS 高德地图(二)(进阶具体使用的细节)
- 高德地图SDK开发(IOS版本)
- 实习第二个项目——彩虹旗传递(和高德地图有关的知识笔记)
- AMapNavi.getInstance(this) 返回值为null 的解决方法
- 有关于高德地图的大头针下落动画。还有就是高德地图的设置
- 融云,发送位置,腾讯地图改为高德地图,SOSOLocationActivity 核心类修改
- 地球坐标,火星坐标,百度坐标转换编码
- 高德地图API学习小记
- 高德地图导航中出现报空的情况
- ios 高德地图 通过索引输出经纬度 根据经纬度 定位
- 2.使用地图API,获取经纬度并传到服务器的可行方法
- 打包前代码混淆
- 高德地图搭建与实现
- Gps坐标转换成 百度坐标 高德坐标
- 地图展示与导航
- swift调用高端地图
- MapKit的学习一
- 在nodejs中进行火星坐标转换
- 高德地图定位INVALID_USER_SCODE解决办法