您的位置:首页 > 移动开发 > IOS开发

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.

下面直接上代码判断手机中是否安装了百度地图、谷歌地图、高德地图:如果安装了相关地图则把调用地图的相关字符串保存为字典,将字典存为数组.

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就可以选择应用来进行导航啦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  地图 导航 ios