當然 如果沒有安裝某個地圖APP 那么對應(yīng)的選項是不會出現(xiàn)的 檢測APP是否安裝 只要調(diào)用下面這個方法就可以了
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"appurlscheme://"]
關(guān)于APP的URL Scheme相關(guān)內(nèi)容這里就不介紹了 大家可以自行去研究
那么我們上圖提到了4個地圖應(yīng)用 分別是
蘋果地圖
百度地圖
高德地圖
谷歌地圖
這些也是當前我們用得最多的幾種地圖了(什么 你們說還有騰訊地圖? 可惜騰訊地圖暫時還不支持URI的方式打開 所以這里就沒列出來 等可以用了我會補上)
下面來對比一下幾種地圖
地圖URL Scheme文檔是否可以跳回到APP
蘋果地圖文檔否
百度地圖baidumap://文檔否
高德地圖iosamap://文檔是
谷歌地圖comgooglemaps://文檔是
蘋果地圖是系統(tǒng)自帶的(而且蘋果地圖最好的方式也不是用URI的方式開打) 所以無需URL Scheme就可以打開的
其次 當跳到地圖APP之后可以跳回是一種很好的體驗(參考微信的跳轉(zhuǎn)) 但是遺憾的是 蘋果地圖和百度地圖都不支持跳回
接下來我們就回到正題 說一說每種地圖的跳轉(zhuǎn)方式
假設(shè)我們有一個指定的目的坐標coordinate而我們自己的APP的URL Scheme是urlScheme名稱是appName
CLLocationCoordinate2D coordinate;NSString *urlScheme;NSString *appName;
蘋果地圖
蘋果地圖可以通過openURL的方式打開
NSString*urlString = [[NSStringstringWithFormat:@"http://maps.apple.com/?daddr=%f,%f&saddr=slat,slng",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding][[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];
但是這種方式不能以當前位置為起點所以不符合我們的要求 網(wǎng)上說可以用下面這種方式 但是我沒成功
NSString*urlString = [[NSStringstringWithFormat:@"http://maps.apple.com/?daddr=%f,%f&saddr=Current+Location",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
但是蘋果提供了另一種方式 使用MKMapItem
MKMapItem*currentLocation = [MKMapItemmapItemForCurrentLocation];MKMapItem*toLocation = [[MKMapItemalloc] initWithPlacemark:[[MKPlacemarkalloc] initWithCoordinate:coordinate addressDictionary:nil]];[MKMapItemopenMapsWithItems:@[currentLocation, toLocation]? ? ? ? ? ? ? ? launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumbernumberWithBool:YES]}];
效果如下
百度地圖
NSString*urlString = [[NSStringstringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];
要注意幾點
origin={{我的位置}}
這個是不能被修改的 不然無法把出發(fā)位置設(shè)置為當前位置
destination=latlng:%f,%f|name=目的地
name=XXXXname這個字段不能省略 否則導(dǎo)航會失敗 而后面的文字則可以隨便填
coord_type=gcj02
coord_type允許的值為bd09ll歼郭、gcj02吩屹、wgs84 如果你APP的地圖SDK用的是百度地圖SDK 請?zhí)頱d09ll 否則 就填gcj02 wgs84你基本是用不上了(關(guān)于地圖加密這里也不多談 請自行學(xué)習(xí))
效果如下
高德地圖
NSString*urlString = [[NSStringstringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];
要注意幾點
sourceApplication=%@&backScheme=%@
sourceApplication代表你自己APP的名稱 會在之后跳回的時候顯示出來 所以必須填寫? backScheme是你APP的URL Scheme? 不填是跳不回來的喲
dev=0
這里填0就行了 跟上面的gcj02一個意思? 1代表wgs84 也用不上
效果如下
退出導(dǎo)航后 會提示是否跳回到APP
谷歌地圖
NSString*urlString = [[NSStringstringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];
要注意幾點
x-source=%@&x-success=%@
跟高德一樣 這里分別代表APP的名稱和URL Scheme
saddr=
這里留空則表示從當前位置觸發(fā)
效果如下 在有多條路線的時候 谷歌地圖會讓你選擇其中一條
選擇之后就進入了導(dǎo)航頁面
騰訊地圖
既然提到了騰訊地圖 那么還是說一下 從網(wǎng)上和官方文檔可以得知 大概調(diào)用的URI如下
NSString*urlString = [[NSStringstringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=%f,%f&coord_type=1&policy=0",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];