在地圖類應用開發中,我們經常有導航這個功能需求。根據導航方式可以分為應用內導航和應用外導航,其中應用內導航指的是使用第三方提供的地圖SDK(高德、百度等)嵌入到我們開發的APP內部。應用外導航指的是以URL Scheme 跳轉的方式,跳轉到對應的地圖APP中,使用對方的導航功能。
不使用 SDK 進行開發,而是直接調用 (百度,高德,系統自帶高德)APP。這樣給了客戶多重選擇。更減少了引入 SDK 使 APP 臃腫的問題。適用于移動設備瀏覽器端應用和移動App應用均可調起iOS版移動應用地圖客戶端
需求:實現應用外導航。通過選項列表(UIAlertController)的方式提供用戶選擇,當用戶既安裝了高德地圖和百度地圖時,則彈出如下圖所示的選項列表。否則用戶安裝了哪個地圖,就增加哪個地圖的選擇項。根據指定起點、終點以及出行方式,調起騰訊地圖APP的路線規劃功能,查詢出行路線,并在地圖中展示。(起點可自動根據定位獲取)
URL 開發文檔地址
1.百度地圖 https://lbsyun.baidu.com/index.php?title=uri/api/ios
2.高德地圖 https://lbs.amap.com/api/amap-mobile/guide/ios/ios-uri-information
2.騰訊地圖 https://lbs.qq.com/webApi/uriV1/uriGuide/uriMobileRoute
環境配置
配置白名單 由于iOS的限制,iOS9之后app想調起高德地圖/百度地圖,必須在自己app設置中配置白名單
配置方法:
1、找到您的Info.plist文件
2、在文件中添加key:LSApplicationQueriesSchemes,類型是Array,如果曾經添加過,無需再次添加。
3、Array中添加一個item,類型為String,值為高德:iosamap / 百度:baidumap/ 騰訊地圖:qqmap。 如圖所示
判斷是否安裝地圖客戶端
步驟 1:通過查看是否可以打開該 scheme, 判斷是否安裝高德/百度地圖
- (BOOL)canOpenURL:(NSURL*)url
步驟 2:判斷是否安裝了高德/百度地圖
配置完成后,您就可以在自己的app中判斷地圖客戶端是否已安裝。
示例代碼如下:
NSURL *scheme = [NSURL URLWithString:@"iosamap://"];
//如果百度地圖是 @"baidumap://" 騰訊地圖是@"qqmap://"
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:scheme];
如果canOpen為YES,則安裝了高德/百度地圖;如果canOpen為NO,則未安裝高德地圖。
注意:蘋果自帶地圖不需要檢測,默認已經安裝
使用
#pragma mark - 調取導航
/// 導航功能方法封裝
/// @param lat 緯度
/// @param lng 經度
/// @param address 地圖顯示目的地
/// @param currentController 在哪個VC彈出
- (void)mapNavigationLat:(double)lat
lng:(double)lng
address:(NSString *)address
currentController:(UIViewController *)currentController{
__block NSString *urlScheme = @"vlife://";//設置本APPurlScheme 可修改
__block NSString *appName = @"vlife";//設置APP名稱 可修改
//設置目的地的經緯度結構體
__block CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(lat,lng);
if (address == nil || address.length == 0) {
address = @"目的地";
}
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]) {
UIAlertAction *action = [UIAlertAction actionWithTitle:@"蘋果地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
toLocation.name = address;
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}];
[alert addAction:action];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
UIAlertAction *action = [UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *badiduStr = [NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=name:%@|latlng:%f,%f&mode=driving&coord_type=gcj02",address,coordinate.latitude, coordinate.longitude];
NSString *urlString = [badiduStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) { NSLog(@"scheme調用結束"); }];
}];
[alert addAction:action];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *gaodeStr = [NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&dlat=%f&dlon=%f&dname=%@&dev=0&t=0",appName,urlScheme,coordinate.latitude, coordinate.longitude,address];
NSURL *myLocationScheme = [NSURL URLWithString:[gaodeStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
[[UIApplication sharedApplication] openURL:myLocationScheme options:@{} completionHandler:^(BOOL success) { NSLog(@"scheme調用結束"); }];
}];
[alert addAction:action];
}
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action];
if (currentController) {
[currentController presentViewController:alert animated:YES completion:nil];
}
}
坐標系轉換問題
1.地球坐標 (WGS84)
國際標準,從GPS設備中取出的數據的坐標系
國際地圖提供商使用的坐標系
2.火星坐標 (GCJ-02) 也叫國測局坐標系
中國標準,從國行移動設備中定位獲取的坐標數據使用這個坐標系
國家規定: 國內出版的各種地圖系統(包括電子形式),必須至少采用GCJ-02對地理位置進行首次加密。高德地圖、騰訊地圖使用
3.百度坐標 (BD-09)
百度標準,百度 SDK,百度地圖,Geocoding 使用
百度在火星坐標上進行了二次加密
根據你使用不同地圖采用的坐標系,進行相應的坐標系轉換處理。不會展示會有偏差。