在你项目的info.plist添加 LSApplicationQueriesSchemes 为KEY 类型为NSArray;添加对应的白名单
高德:iosamap百度:baidumap
- (void)click:(UIButton*)button{
// 百度地图与高德地图、苹果地图采用的坐标系不一样,高德和苹果只能用地名不能用后台返回的坐标
CGFloatlatitude =36.547901; // 纬度
CGFloatlongitude =104.258354;// 经度
self.address = [[NSString alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:(CLLocationDegrees)longitude];
CLGeocoder * geocoder = [[CLGeocoder alloc]init];
[geocoderreverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if(error ==nil&& [placemarkscount] >0) {
//这时的placemarks数组里面只有一个元素
CLPlacemark* placemark = [placemarksfirstObject];
NSLog(@"%@",placemark.addressDictionary); //根据经纬度会输出该经纬度下的详细地址 国家 地区 街道
self.address = [NSString stringWithFormat:@"%@",placemark.addressDictionary[@"FormattedAddressLines"][0]] ;
[self openAlert];
}
}];
}
- (void)openAlert{
CGFloatlatitude =36.547901; // 纬度
CGFloatlongitude =104.258354;
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:(CLLocationDegrees)longitude];
// 打开地图的优先级顺序:百度地图->高德地图->苹果地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
// 百度地图
// 起点为“我的位置”,终点为后台返回的坐标
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=%f,%f&mode=riding&src=%@", latitude, longitude,self.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL*url = [NSURLURLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
}else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
// 高德地图
// 起点为“我的位置”,终点为后台返回的address
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&sname=%@&did=BGVIS2&dname=%@&dev=0&t=0",@"我的位置",self.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com"]]){
// 苹果地图
// 起点为“我的位置”,终点为后台返回的address
NSString *urlString = [[NSString stringWithFormat:@"http://maps.apple.com/?daddr=%@",self.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
// 没有安装上面三种地图APP,弹窗提示安装地图APP
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请安地图APP" message:@"建议安装百度地图APP" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertVC animated:NO completion:nil];
}
}