1.獲取地圖導(dǎo)航路線坐標(biāo)
https://lbs.amap.com/console/show/picker 高德地圖坐標(biāo)拾取器
http://api.map.baidu.com/lbsapi/getpoint/index.html 百度地圖拾取器
2.坐標(biāo)系轉(zhuǎn)換
高德/蘋果/騰訊地圖 用的是一套坐標(biāo)辉词,而百度使用自家的坐標(biāo)习蓬,有點(diǎn)坑蹦玫,需要轉(zhuǎn)換一下坐標(biāo)系 即高德坐標(biāo)轉(zhuǎn)百度坐標(biāo) 菩貌,用以下方法轉(zhuǎn)換 無須復(fù)雜計算,親測可用:
- (CLLocationCoordinate2D)getBaiDuCoordinateByGaoDeCoordinate:(CLLocationCoordinate2D)coordinate
{
return CLLocationCoordinate2DMake(coordinate.latitude + 0.006, coordinate.longitude + 0.0065);
}
3. iOS9系統(tǒng)以上一定要配置info.plist 白名單
<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string>
<string>qqmap</string>
<string>iosamap</string>
</array>
4.跳轉(zhuǎn)調(diào)用代碼
//開始導(dǎo)航
- (IBAction)onClickButton {
JHSheetActionView *sheetView= [[JHSheetActionView alloc] init];
sheetView.sheetItems=@[@[@"Apple 地圖",@"高德地圖",@"百度地圖",@"騰訊地圖"],@[@"取消"]];
sheetView.callBackCellForRowIndex = ^(NSInteger index) {
NSString *urlsting=nil;
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(39.918058, 116.397026);
NSString * name=@"故宮博物館";
switch (index) {
//Apple 地圖
case 1:{
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *tolocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
tolocation.name=name;
[MKMapItem openMapsWithItems:@[currentLocation,tolocation]launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}
break;
//高德地圖
case 2:{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
urlsting =[[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&did=BGVIS2&dlat=%lf&dlon=%lf&dname=%@&dev=0&t=0",coordinate.latitude,coordinate.longitude,name] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
}
break;
//百度地圖
case 3:{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
//轉(zhuǎn)換百度坐標(biāo)
CLLocationCoordinate2D xcoordinate=[self getBaiDuCoordinateByGaoDeCoordinate:coordinate];
urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin=我的位置&destination=latlng:%f,%f|name:%@&mode=driving&coord_type=gcj02",xcoordinate.latitude,xcoordinate.longitude,name] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
}
break;
//騰訊地圖
case 4:{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
urlsting =[[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&to=%@&tocoord=%lf,%lf&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77",name,coordinate.latitude,coordinate.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
}
break;
default:
break;
}
//打開應(yīng)用內(nèi)跳轉(zhuǎn)
if(!urlsting) return;
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting] options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];
}
};
[sheetView showView];
}
其中使用了 JHSheetActionView 控件 可以使用pod 'JHSheetActionView ' 添加