整理一下iOS跳轉(zhuǎn)第三方地圖導(dǎo)航的方法
1.獲取定位權(quán)限
<key>NSLocationAlwaysUsageDescription</key>
<string>根據(jù)您的位置來(lái)提供服務(wù)</string>
<key>NSLocationUsageDescription</key>
<string>根據(jù)您的位置來(lái)提供服務(wù)</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>根據(jù)您的位置來(lái)提供服務(wù)</string>
2.添加跳轉(zhuǎn)第三方地圖App的白名單
<key>LSApplicationQueriesSchemes</key>
<array>
<string>iosamap</string>
<string>baidumap</string>
<string>qqmap</string>
</array>
3.代碼示例
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.whiteColor;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = 20202203;
button.frame = CGRectMake((self.view.frame.size.width - 200) / 2, 100, 200, 40);
button.titleLabel.font = [UIFont systemFontOfSize:13];
[button setTitle:@"開(kāi)始導(dǎo)航" forState:UIControlStateNormal];
[button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[button addTarget:self action:@selector(gpsAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)gpsAction:(UIButton *)button {
CLLocationDegrees latitude = 31.178513;
CLLocationDegrees longitude = 121.494612;
CLLocationCoordinate2D endLocation = CLLocationCoordinate2DMake(latitude, longitude);
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"請(qǐng)選擇導(dǎo)航" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
NSArray *titles = @[@"百度地圖", @"騰訊地圖", @"高德地圖", @"蘋(píng)果地圖"];
for (int i = 0; i < titles.count; i++) {
NSString *title = titles[i];
UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self navThirdMapWithLocation:endLocation andTitle:title];
}];
[alertVC addAction:action];
}
UIAlertAction *cancleAct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertVC addAction:cancleAct];
[self presentViewController:alertVC animated:YES completion:nil];
}
-(void)navThirdMapWithLocation:(CLLocationCoordinate2D)endLocation andTitle:(NSString *)andTitle {
/**
<key>LSApplicationQueriesSchemes</key>
<array>
<string>iosamap</string>
<string>baidumap</string>
<string>qqmap</string>
</array>
* 1奏黑、地球坐標(biāo) :( 代號(hào):GPS、WGS84 )--- 有W就是世界通用的
* 蘋(píng)果的 CLLocationManager 獲取的坐標(biāo)
* 2寇损、火星坐標(biāo): (代號(hào):GCJ-02)--- * G國(guó)家 C測(cè)繪 J局 02年測(cè)繪的*
* 高德地圖、騰訊地圖羹呵、阿里云地圖优构、靈圖51地圖
* 現(xiàn)在蘋(píng)果系統(tǒng)自帶的地圖使用的是高德地圖,所以蘋(píng)果地帶的地圖應(yīng)用界牡,用的是GCJ-02的坐標(biāo)系統(tǒng)簿寂。但是代碼中CLLocationManager獲取到的是WGS84坐標(biāo)系的坐標(biāo)
* 3、其他坐標(biāo) :百度坐標(biāo)系統(tǒng) (代號(hào):BD-09)
* 使用 BD-09 坐標(biāo)系統(tǒng)的產(chǎn)品有: 百度地圖
*/
NSString *destination = @"世博大道";
NSString *mapType = andTitle;
NSString *openUrl = @"";
NSString *downloadUrl = @"";
NSString *routeUrl = @"";
//將h5傳來(lái)的WGS84轉(zhuǎn)GCJ-02坐標(biāo)系宿亡,再在騰訊常遂,高德,蘋(píng)果地圖上使用
CLLocationCoordinate2D coord = [WWLocationConverter wgs84ToGcj02:endLocation];
if ([@"百度地圖" isEqual:mapType]) {
//將h5傳來(lái)的WGS84轉(zhuǎn)BD09坐標(biāo)系挽荠,再在百度地圖上使用
coord = [WWLocationConverter wgs84ToBd09:endLocation];
openUrl = @"baidumap://";
routeUrl = [NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=bd09ll", coord.latitude, coord.longitude];
downloadUrl = @"itms-apps://itunes.apple.com/cn/app/id452186370?mt=8";
} else if ([@"騰訊地圖" isEqual:mapType]) {
openUrl = @"qqmap://";
routeUrl = [NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&to=%@&tocoord=%f,%f&coord_type=1&referer={ios.blackfish.XHY}", destination, coord.latitude, coord.longitude];
downloadUrl = @"itms-apps://itunes.apple.com/app/id481623196?mt=8";
} else if ([@"高德地圖" isEqual:mapType]) {
openUrl = @"iosamap://";
routeUrl = [NSString stringWithFormat:@"iosamap://path?sourceApplication=ios.blackfish.XHY&dlat=%f&dlon=%f&dname=%@&style=2", coord.latitude, coord.longitude, destination];
downloadUrl = @"itms-apps://itunes.apple.com/app/id461703208?mt=8";
} else if ([@"蘋(píng)果地圖" isEqual:mapType]) {
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *tolocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coord addressDictionary:nil]];
tolocation.name = destination;
[MKMapItem openMapsWithItems:@[currentLocation,tolocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
return;
}
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:openUrl]]) {
NSString *urlString = [routeUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
} else {
UIAlertController *altVC = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"你尚未安裝%@克胳,是否立刻下載?", mapType] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//跳轉(zhuǎn)到地圖App下載
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:downloadUrl] options:@{} completionHandler:nil];
}];
[altVC addAction:okAction];
UIAlertAction *canceAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[altVC addAction:canceAction];
[self presentViewController:altVC animated:YES completion:nil];
}
}
以上方式解決了跳轉(zhuǎn)第三方地圖導(dǎo)航的問(wèn)題平绩,本篇GitHubDemo,希望能幫到你...
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者