在地圖類應(yīng)用開(kāi)發(fā)中措拇,我們經(jīng)常有導(dǎo)航這個(gè)功能需求。根據(jù)導(dǎo)航方式可以分為應(yīng)用內(nèi)導(dǎo)航和應(yīng)用外導(dǎo)航,其中應(yīng)用內(nèi)導(dǎo)航指的是使用第三方提供的地圖SDK(高德问拘、百度等)嵌入到我們開(kāi)發(fā)的APP內(nèi)部。應(yīng)用外導(dǎo)航指的是以URL Scheme 跳轉(zhuǎn)的方式惧所,跳轉(zhuǎn)到對(duì)應(yīng)的地圖APP中骤坐,使用對(duì)方的導(dǎo)航功能。
不使用 SDK 進(jìn)行開(kāi)發(fā)下愈,而是直接調(diào)用 (百度纽绍,高德,系統(tǒng)自帶高德)APP势似。這樣給了客戶多重選擇拌夏。更減少了引入 SDK 使 APP 臃腫的問(wèn)題僧著。適用于移動(dòng)設(shè)備瀏覽器端應(yīng)用和移動(dòng)App應(yīng)用均可調(diào)起iOS版移動(dòng)應(yīng)用地圖客戶端
需求:實(shí)現(xiàn)應(yīng)用外導(dǎo)航。通過(guò)選項(xiàng)列表(UIAlertController)的方式提供用戶選擇障簿,當(dāng)用戶既安裝了高德地圖和百度地圖時(shí)盹愚,則彈出如下圖所示的選項(xiàng)列表。否則用戶安裝了哪個(gè)地圖卷谈,就增加哪個(gè)地圖的選擇項(xiàng)杯拐。根據(jù)指定起點(diǎn)、終點(diǎn)以及出行方式世蔗,調(diào)起騰訊地圖APP的路線規(guī)劃功能端逼,查詢出行路線,并在地圖中展示污淋。(起點(diǎn)可自動(dòng)根據(jù)定位獲取)
URL 開(kāi)發(fā)文檔地址
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
環(huán)境配置
配置白名單 由于iOS的限制顶滩,iOS9之后app想調(diào)起高德地圖/百度地圖,必須在自己app設(shè)置中配置白名單
配置方法:
1寸爆、找到您的Info.plist文件
2礁鲁、在文件中添加key:LSApplicationQueriesSchemes,類型是Array赁豆,如果曾經(jīng)添加過(guò)仅醇,無(wú)需再次添加。
3魔种、Array中添加一個(gè)item析二,類型為String,值為高德:iosamap / 百度:baidumap/ 騰訊地圖:qqmap节预。 如圖所示
判斷是否安裝地圖客戶端
步驟 1:通過(guò)查看是否可以打開(kāi)該 scheme, 判斷是否安裝高德/百度地圖
- (BOOL)canOpenURL:(NSURL*)url
步驟 2:判斷是否安裝了高德/百度地圖
配置完成后叶摄,您就可以在自己的app中判斷地圖客戶端是否已安裝。
示例代碼如下:
NSURL *scheme = [NSURL URLWithString:@"iosamap://"];
//如果百度地圖是 @"baidumap://" 騰訊地圖是@"qqmap://"
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:scheme];
如果canOpen為YES安拟,則安裝了高德/百度地圖蛤吓;如果canOpen為NO,則未安裝高德地圖糠赦。
注意:蘋果自帶地圖不需要檢測(cè)会傲,默認(rèn)已經(jīng)安裝
使用
#pragma mark - 調(diào)取導(dǎo)航
/// 導(dǎo)航功能方法封裝
/// @param lat 緯度
/// @param lng 經(jīng)度
/// @param address 地圖顯示目的地
/// @param currentController 在哪個(gè)VC彈出
- (void)mapNavigationLat:(double)lat
lng:(double)lng
address:(NSString *)address
currentController:(UIViewController *)currentController{
__block NSString *urlScheme = @"vlife://";//設(shè)置本APPurlScheme 可修改
__block NSString *appName = @"vlife";//設(shè)置APP名稱 可修改
//設(shè)置目的地的經(jīng)緯度結(jié)構(gòu)體
__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調(diào)用結(jié)束"); }];
}];
[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調(diào)用結(jié)束"); }];
}];
[alert addAction:action];
}
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action];
if (currentController) {
[currentController presentViewController:alert animated:YES completion:nil];
}
}
坐標(biāo)系轉(zhuǎn)換問(wèn)題
1.地球坐標(biāo) (WGS84)
國(guó)際標(biāo)準(zhǔn),從GPS設(shè)備中取出的數(shù)據(jù)的坐標(biāo)系
國(guó)際地圖提供商使用的坐標(biāo)系
2.火星坐標(biāo) (GCJ-02) 也叫國(guó)測(cè)局坐標(biāo)系
中國(guó)標(biāo)準(zhǔn)愉棱,從國(guó)行移動(dòng)設(shè)備中定位獲取的坐標(biāo)數(shù)據(jù)使用這個(gè)坐標(biāo)系
國(guó)家規(guī)定: 國(guó)內(nèi)出版的各種地圖系統(tǒng)(包括電子形式)唆铐,必須至少采用GCJ-02對(duì)地理位置進(jìn)行首次加密。高德地圖奔滑、騰訊地圖使用
3.百度坐標(biāo) (BD-09)
百度標(biāo)準(zhǔn),百度 SDK顺少,百度地圖朋其,Geocoding 使用
百度在火星坐標(biāo)上進(jìn)行了二次加密
根據(jù)你使用不同地圖采用的坐標(biāo)系王浴,進(jìn)行相應(yīng)的坐標(biāo)系轉(zhuǎn)換處理。不會(huì)展示會(huì)有偏差梅猿。