一、info.plist文件添加Schemes

1
2
3
4
5
6
7
<key>LSApplicationQueriesSchemes</key>
<array>
<string>iosamap</string>
<string>baidumap</string>
<string>comgooglemaps</string>
<string>qqmap</string>
</array>

二、获取当前经纬度和地址

三、开始导航

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请选择导航" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

NSURL * apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
float latitude = [_model.latitude floatValue];
float longitude = [_model.longitude floatValue];
float locLaitude = self.localLatitude;
float locLongitude = self.localLongitude;
NSString *locAddress = self.localAddress;
NSString *address = _model.address;
UIAlertAction *appleAction = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([[UIApplication sharedApplication] canOpenURL:apple_App]) {
NSString *urlString=[NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",locLaitude ,locLongitude,latitude,longitude ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
[SVProgressHUD showSuccessWithStatus:@"请先安装苹果地图客户端"];
}
}];

NSURL * gaode_App = [NSURL URLWithString:@"iosamap://"];
UIAlertAction *gaodeAction = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([[UIApplication sharedApplication] canOpenURL:gaode_App]) {
NSString *urlString=[[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&slat=%f&slon=%f&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&t=0",locAddress,locLaitude,locLongitude,locAddress,latitude,longitude,address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
[SVProgressHUD showSuccessWithStatus:@"请先安装高德地图客户端"];
}
}];

NSURL * baidu_App = [NSURL URLWithString:@"baidumap://"];
UIAlertAction *baiduAction = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([[UIApplication sharedApplication] canOpenURL:baidu_App]) {
NSString *urlString=[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",latitude,longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
[SVProgressHUD showSuccessWithStatus:@"请先安装百度地图客户端"];
}
}];

NSURL * tencent_App = [NSURL URLWithString:@"qqmap://"];
UIAlertAction *tencentAction = [UIAlertAction actionWithTitle:@"腾讯地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([[UIApplication sharedApplication] canOpenURL:tencent_App]) {
NSString *urlString=[[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=%f,%f&tocoord=%f,%f&policy=1",locLaitude,locLongitude,latitude,longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
[SVProgressHUD showSuccessWithStatus:@"请先安装腾讯地图客户端"];
}
}];

NSURL * google_App = [NSURL URLWithString:@"comgooglemaps://"];
UIAlertAction *googleAction = [UIAlertAction actionWithTitle:@"谷歌地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([[UIApplication sharedApplication] canOpenURL:google_App]) {
NSString *urlString=[[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"***",@"ChuanLaoDa",latitude,locLongitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

}else{
[SVProgressHUD showSuccessWithStatus:@"请先安装谷歌地图客户端"];
}
}];

[alertVC addAction:cancelAction];
[alertVC addAction:appleAction];
[alertVC addAction:gaodeAction];
[alertVC addAction:baiduAction];
[alertVC addAction:tencentAction];
[alertVC addAction:googleAction];
[self presentViewController:alertVC animated:false completion:nil];
iOS
Swift 定位权限封装,坐标转换仿App Store转场动画 Swift语言实现