IOS 不集成SDK使用URL方式打开地图导航 谷歌 百度 高德
2016-05-13 15:50
645 查看
使用URL方式打开其他地图导航工具需要设置:
1.在IOS9.0以后苹果把HTTP更改为了HTTPS请求方式,需要在info.plist中添加 Application requires iPhone environment字段,类型为Boolean.设置为YES. 这个字段的意思是允许使用HTTP的请求方式.
2.需要为连接的地图应用添加Scheme白名单,在Info.plist中添加 LSApplicationQueriesSchemes字段.设置为数组类型.将要添加的APP应用名称添加到数组中去.百度的名称为:baidumap,谷歌的名称为:comgooglemaps,高德的名称为:iosamap.
下面直接上代码判断手机中是否安装了百度地图、谷歌地图、高德地图:如果安装了相关地图则把调用地图的相关字符串保存为字典,将字典存为数组.
下面我们使用刚刚保存的数据来创建一个ActionSheet,让用户选择使用哪种方式导航:
1.在IOS9.0以后苹果把HTTP更改为了HTTPS请求方式,需要在info.plist中添加 Application requires iPhone environment字段,类型为Boolean.设置为YES. 这个字段的意思是允许使用HTTP的请求方式.
2.需要为连接的地图应用添加Scheme白名单,在Info.plist中添加 LSApplicationQueriesSchemes字段.设置为数组类型.将要添加的APP应用名称添加到数组中去.百度的名称为:baidumap,谷歌的名称为:comgooglemaps,高德的名称为:iosamap.
下面直接上代码判断手机中是否安装了百度地图、谷歌地图、高德地图:如果安装了相关地图则把调用地图的相关字符串保存为字典,将字典存为数组.
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){ NSString * urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=transit",self.startCoor.latitude, self.startCoor.longitude, self.endCoor.latitude, self.endCoor.longitude,self.location]; NSDictionary * dic = @{@"name": @"百度地图",@"url": urlString}; [self.availableMaps addObject:dic]; } if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) { NSString * urlString = [NSString stringWithFormat:@"iosamap://path?sourceApplication=公司名称&slat=%f&slon=%f&sname=我的位置&dlat=%f&dlon=%f&dname=%@&dev=0&style=0",self.startCoor.latitude,self.startCoor.longitude,self.endCoor.latitude,self.endCoor.longitude,self.location]; NSDictionary *dic = @{@"name":@"高德地图",@"url":urlString}; [self.availableMaps addObject:dic]; } if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) { NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%@&directionsmode=driving",self.location]; NSDictionary *dic = @{@"name": @"Google Maps",@"url":urlString}; [self.availableMaps addObject:dic]; }
下面我们使用刚刚保存的数据来创建一个ActionSheet,让用户选择使用哪种方式导航:
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; for (int maps =0; maps<self.availableMaps.count; maps++) {
NSDictionary * point =self.availableMaps[maps]; [alertController addAction: [UIAlertAction actionWithTitle:point[@"name"] style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){ NSString *urlString = point[@"url"]; urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlString]; [[UIApplication sharedApplication] openURL:url]; }]]; } [alertController addAction: [UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:nil]]; [self presentViewController: alertController animated: YES completion: nil];接下来用户只要调出ActionSheet就可以选择应用来进行导航啦!
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- jQuery实现下滑菜单导航效果代码
- jquery实现的仿天猫侧导航tab切换效果
- PHP的栏目导航程序
- 百度地图经纬度转换到腾讯地图/Google 对应的经纬度
- jquery实现向下滑出的二级导航下滑菜单效果
- 基于jQuery实现以手风琴方式展开和折叠导航菜单
- JS+CSS实现六级网站导航主菜单效果
- JS实现网页顶部向下滑出的全国城市切换导航效果
- 使用Sticky组件实现带sticky效果的tab导航和滚动导航的方法
- js实现显示当前状态的导航效果代码
- javascript实现当前页导航激活的方法
- JS+CSS实现的日本门户网站经典选项卡导航效果
- JS实现自动变化的导航菜单效果代码