iOS呼叫各大地圖APP導航,進行路線規劃
最近收到了這麼一個需求,就是支援目前主流的地圖APP導航,也就是說跳轉至第三方應用。說實在的一開始我是拒絕的,不過最後還是做了,全是體力活,為了方便大家,我就終結一下,以供參考:
首先是判斷APP是否安裝程式碼如下:
[[UIApplicationsharedApplication] canOpenURL:[NSURLURLWithString:@"iosamap://"]
然後就是新增各個地圖APP的白名單:
百度地圖:baidumap:
高德地圖:iosamap:
騰訊地圖:qqmap:
谷歌地圖:comgooglemapsurl:
系統地圖就不用這麼麻煩了,直接這樣就好:
CLLocationCoordinate2D
MKMapItem *currentLocation = [MKMapItemmapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItemalloc] initWithPlacemark:[[MKPlacemarkalloc] initWithCoordinate:endCoor addressDictionary:nil]];
toLocation.name = [NSStringstringWithFormat:@"到 %@", 目的地];
[MKMapItemopenMapsWithItems
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumbernumberWithBool:YES]}];
其他地圖的開啟如下:
百度地圖:
NSString *stringURL = [NSStringstringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=%f,%f&mode=driving"
目的地.latitude, 目的地.longitude];
[[UIApplicationsharedApplication] openURL:url]
高德地圖:
NSString *urlString = [[NSStringstringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&sname=%@&dname=%@&dev=0&m=0&t=0&sid=BGVIS1&did=BGVIS2&dlat=%lf&dlon=%lf",@"APP名稱", @"iosamap", @"我的位置",目的地,endCoor.latitude, endCoor.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:urlString]];
騰訊地圖:
NSString *urlString =[[NSStringstringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&to=%@&tocoord=%lf,%lf&policy=1&referer=tengxun",目的地,endCoor.latitude,endCoor.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];
谷歌地圖:(國內谷歌地圖無法訪問得要輪子哦)
NSString *urlString = [[NSString stringWithFormat:@"comgooglemapsurl://www.google.com/maps/preview/@%lf,%lf,6z",endCoor.latitude, endCoor.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
以上就是了,至於對應的連結,當時忘記記錄了,建議使用谷歌搜一下,都可以看到。