iOS呼叫高德地圖導航
阿新 • • 發佈:2019-01-08
在iOS開發地圖模組中,有需要用到導航的功能,尤其類似一些送快遞、外賣等軟體,除了需要展示路線到地圖中,還需要有一個導航按鈕。一般導航功能分兩類:一類是在本APP內部呼叫高德API的導航頁面,即在APP內部整合導航模組,此類導航頁面可以自己定製介面,但基本功能都是呼叫高德封裝好的方法即可。另一類就是點選導航按鈕,跳轉到手機內裝的地圖類APP裡去進行導航。
今天我要說的是第二類,點選跳轉到地圖APP裡去導航。因為第一類導航,在對應的API文件和Demo裡已經有詳細的文件來說明如何整合。
下面是點選導航按鈕需要的程式碼:
//MARK:導航 - (void)navBtnClicked:(UIButton *)sender { // 後臺返回的目的地座標是百度地圖的 // 百度地圖與高德地圖、蘋果地圖採用的座標系不一樣,故高德和蘋果只能用地名不能用後臺返回的座標 CGFloat latitude = (CGFloat)endLat; // 緯度 CGFloat longitude = (CGFloat)endLng; // 經度 // __block NSString *address = @"";// 送達地址 self.address = [[NSString alloc] init]; CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:(CLLocationDegrees)longitude]; CLGeocoder * geocoder = [[CLGeocoder alloc]init]; [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { if (error == nil && [placemarks count] > 0) { //這時的placemarks數組裡面只有一個元素 CLPlacemark * placemark = [placemarks firstObject]; NSLog(@"%@",placemark.addressDictionary); //根據經緯度會輸出該經緯度下的詳細地址 國家 地區 街道 之類的 self.address = [NSString stringWithFormat:@"%@",placemark.addressDictionary[@"FormattedAddressLines"][0]] ; [self openAlert]; } }]; } - (void)openAlert { CGFloat latitude = (CGFloat)endLat; // 緯度 CGFloat longitude = (CGFloat)endLng; // 經度 CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:(CLLocationDegrees)longitude]; // 開啟地圖的優先順序順序:百度地圖->高德地圖->蘋果地圖 if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) { // 高德地圖 // 起點為“我的位置”,終點為後臺返回的address NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&sname=%@&did=BGVIS2&dname=%@&dev=0&t=0",@"我的位置",self.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:nil completionHandler:^(BOOL success) { }]; }else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) { // 百度地圖 // 起點為“我的位置”,終點為後臺返回的座標 NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=%f,%f&mode=riding&src=快健康快遞", latitude, longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlString]; [[UIApplication sharedApplication] openURL:url options:nil completionHandler:^(BOOL success) { }]; }else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com"]]){ // 蘋果地圖 // 起點為“我的位置”,終點為後臺返回的address NSString *urlString = [[NSString stringWithFormat:@"http://maps.apple.com/?daddr=%@",self.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:nil completionHandler:^(BOOL success) { }]; }else{ // 快遞員沒有安裝上面三種地圖APP,彈窗提示安裝地圖APP UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"請安裝地圖APP" message:@"建議安裝高德地圖APP" preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController:alertVC animated:NO completion:nil]; } }
以上就是設定導航的一種方法,此外,不願意敲程式碼的同學可以直接在info.plist裡直接配置幾種地圖APP的跳轉url,這種方法可以達到以上同樣的效果。
另外,這種呼叫方式的一個問題我還未解決,就是在直接跳轉高德APP時,終點位置首先會彈出一個位置列表讓你點選導航終點,有時專案可能不需要這種周邊搜尋選擇終點,但是我還未找到方法如何避免這個搜尋列表的展示,知道解決辦法的大神希望幫忙解答一下。