1. 程式人生 > >iOS 調起地圖App進行導航(百度,高德,系統自帶高德)URL API方式

iOS 調起地圖App進行導航(百度,高德,系統自帶高德)URL API方式

在開發中需要用到地圖。有兩種方法,一種是匯入地圖 (百度,高德)地圖 SDK,一種是使用調起 客戶端。

但是需要用到一些高階功能的時候,如果使用 SDK 開發,就是一件折磨人的事情。 SDK 還是不如直接使用他們原生的 APP 來的好!

以我在開發中使用導航功能為例,我就沒有使用 SDK 進行開發,而是直接呼叫 (百度,高德,系統自帶高德)APP。這樣還給了客戶多重選擇。更加減少了引入 SDK 使 APP 臃腫的問題。如果使用百度或高德的調起方法配置起來感覺比較麻煩 ,還好他們都提供了 URL API 的方式,這種方式覺得稍微簡單一點

下面將具體使用貼出程式碼如下:更多使用參考(百度,高德)官方文件

1,App 調起百度地圖路線規劃進行導航。百度 URL API :http://lbsyun.baidu.com/index.php?title=uri/api/ios

  1. #pragma mark ------------------------------ 導航 - 百度
  2. -(void) onDaoHangForBaiDuMap  
  3. {  
  4.     //    百度地圖如何調起APP進行導航
  5. //    mode  導航模式,固定為transit、driving、walking,分別表示公交、駕車和步行
  6.     NSString * modeBaiDu = @"driving"
    ;  
  7.     switch (_seleIndex) {  
  8.         case 1:  
  9.         {  
  10.             modeBaiDu = @"transit";  
  11.         }  
  12.             break;  
  13.         case 2:  
  14.         {  
  15.             modeBaiDu = @"driving";  
  16.         }  
  17.             break;  
  18.         case 3:  
  19.         {  
  20.             modeBaiDu = @"walking"
    ;  
  21.         }  
  22.             break;  
  23.         default:  
  24.             break;  
  25.     }  
  26.     NSString *url = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%lf,%lf&destination=%f,%f&mode=%@&src=公司|APP",[SingleObject shareSingleObject].currentCoordinate.latitude,[SingleObject shareSingleObject].currentCoordinate.longitude,self.location.latitude,self.location.longitude,modeBaiDu] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;  
  27. //    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  28.         if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]])// -- 使用 canOpenURL:[NSURL URLWithString:@"baidumap://"] 判斷不明白為什麼為否。
  29.         {  
  30.             [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];  
  31.         }else{  
  32.             [[[UIAlertView alloc]initWithTitle:@"沒有安裝百度地圖" message:@"" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil] show];  
  33.         }  
  34. }  

2,App 調起高德地圖路線規劃進行導航。高德 URL  API:http://lbs.amap.com/api/uri-api/ios-uri-explain/

  1. #pragma mark ------------------------------ 導航 - 高德
  2. -(void) onDaoHangForGaoDeMap  
  3. {  
  4. //    m 駕車:0:速度最快,1:費用最少,2:距離最短,3:不走高速,4:躲避擁堵,5:不走高速且避免收費,6:不走高速且躲避擁堵,7:躲避收費和擁堵,8:不走高速躲避收費和擁堵 公交:0:最快捷,2:最少換乘,3:最少步行,5:不乘地鐵 ,7:只坐地鐵 ,8:時間短  是
  5. //    t = 0:駕車 =1:公交 =2:步行
  6.     NSString * t = @"0";  
  7.     switch (_seleIndex) {  
  8.         case 1:  
  9.         {  
  10.             t = @"1";  
  11.         }  
  12.             break;  
  13.         case 2:  
  14.         {  
  15.             t = @"0";  
  16.         }  
  17.             break;  
  18.         case 3:  
  19.         {  
  20.             t = @"2";  
  21.         }  
  22.             break;  
  23.         default:  
  24.             break;  
  25.     }  
  26.     //起點
  27.     CLLocation * location = [[CLLocation alloc]initWithLatitude:[SingleObject shareSingleObject].currentCoordinate.latitude longitude:[SingleObject shareSingleObject].currentCoordinate.longitude];  
  28.     location = [location locationMarsFromBaidu];  
  29.     CLLocationCoordinate2D coor =location.coordinate;  
  30.     //目的地的位置
  31.     CLLocation * location2 = [[CLLocation alloc]initWithLatitude:self.location.latitude longitude:self.location.longitude];  
  32.     location2 = [location2 locationMarsFromBaidu];  
  33.     CLLocationCoordinate2D coor2 =location2.coordinate;  
  34. //    導航 URL:iosamap://navi?sourceApplication=%@&poiname=%@&lat=%lf&lon=%lf&dev=0&style=0",@"ABC"
  35. //    路徑規劃 URL:iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=39.92848272&slon=116.39560823&sname=A&did=BGVIS2&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&m=0&t=0
  36.     // -- 不能直接讓使用者進入導航,應該給使用者更多的選擇,所以先進行路徑規劃
  37.     NSString *url = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=%lf&slon=%lf&sname=我的位置&did=BGVIS2&dlat=%lf&dlon=%lf&dname=%@&dev=0&m=0&t=%@",coor.latitude,coor.longitude, coor2.latitude,coor2.longitude,self.mainTitle,t] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
  38.     if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]) // -- 使用 canOpenURL:[NSURL URLWithString:@"iosamap://"] 判斷不明白為什麼為否。
  39.     {  
  40.         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];  
  41.     }else{  
  42.         [[[UIAlertView alloc]initWithTitle:@"沒有安裝高德地圖" message:@"" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil] show];  
  43.     }  
  44. }  

1,App 調起系統自帶高德地圖路線規劃進行導航。

  1. #pragma mark ------------------------------ 導航 - iosMap
  2. -(void) onDaoHangForIOSMap  
  3. {  
  4.     //起點
  5.     CLL