iOS 調起地圖App進行導航(百度,高德,系統自帶高德)URL API方式
阿新 • • 發佈:2019-01-18
在開發中需要用到地圖。有兩種方法,一種是匯入地圖 (百度,高德)地圖 SDK,一種是使用調起 客戶端。
但是需要用到一些高階功能的時候,如果使用 SDK 開發,就是一件折磨人的事情。 SDK 還是不如直接使用他們原生的 APP 來的好!
以我在開發中使用導航功能為例,我就沒有使用 SDK 進行開發,而是直接呼叫 (百度,高德,系統自帶高德)APP。這樣還給了客戶多重選擇。更加減少了引入 SDK 使 APP 臃腫的問題。如果使用百度或高德的調起方法配置起來感覺比較麻煩 ,還好他們都提供了 URL API 的方式,這種方式覺得稍微簡單一點
下面將具體使用貼出程式碼如下:更多使用參考(百度,高德)官方文件
1,App 調起百度地圖路線規劃進行導航。百度 URL API :http://lbsyun.baidu.com/index.php?title=uri/api/ios
- #pragma mark ------------------------------ 導航 - 百度
- -(void) onDaoHangForBaiDuMap
- {
- // 百度地圖如何調起APP進行導航
- // mode 導航模式,固定為transit、driving、walking,分別表示公交、駕車和步行
-
NSString * modeBaiDu = @"driving"
- switch (_seleIndex) {
- case 1:
- {
- modeBaiDu = @"transit";
- }
- break;
- case 2:
- {
- modeBaiDu = @"driving";
- }
- break;
- case 3:
- {
-
modeBaiDu = @"walking"
- }
- break;
- default:
- break;
- }
- 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] ;
- // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]])// -- 使用 canOpenURL:[NSURL URLWithString:@"baidumap://"] 判斷不明白為什麼為否。
- {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- }else{
- [[[UIAlertView alloc]initWithTitle:@"沒有安裝百度地圖" message:@"" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil] show];
- }
- }
2,App 調起高德地圖路線規劃進行導航。高德 URL API:http://lbs.amap.com/api/uri-api/ios-uri-explain/
- #pragma mark ------------------------------ 導航 - 高德
- -(void) onDaoHangForGaoDeMap
- {
- // m 駕車:0:速度最快,1:費用最少,2:距離最短,3:不走高速,4:躲避擁堵,5:不走高速且避免收費,6:不走高速且躲避擁堵,7:躲避收費和擁堵,8:不走高速躲避收費和擁堵 公交:0:最快捷,2:最少換乘,3:最少步行,5:不乘地鐵 ,7:只坐地鐵 ,8:時間短 是
- // t = 0:駕車 =1:公交 =2:步行
- NSString * t = @"0";
- switch (_seleIndex) {
- case 1:
- {
- t = @"1";
- }
- break;
- case 2:
- {
- t = @"0";
- }
- break;
- case 3:
- {
- t = @"2";
- }
- break;
- default:
- break;
- }
- //起點
- CLLocation * location = [[CLLocation alloc]initWithLatitude:[SingleObject shareSingleObject].currentCoordinate.latitude longitude:[SingleObject shareSingleObject].currentCoordinate.longitude];
- location = [location locationMarsFromBaidu];
- CLLocationCoordinate2D coor =location.coordinate;
- //目的地的位置
- CLLocation * location2 = [[CLLocation alloc]initWithLatitude:self.location.latitude longitude:self.location.longitude];
- location2 = [location2 locationMarsFromBaidu];
- CLLocationCoordinate2D coor2 =location2.coordinate;
- // 導航 URL:iosamap://navi?sourceApplication=%@&poiname=%@&lat=%lf&lon=%lf&dev=0&style=0",@"ABC"
- // 路徑規劃 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
- // -- 不能直接讓使用者進入導航,應該給使用者更多的選擇,所以先進行路徑規劃
- 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];
- if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]) // -- 使用 canOpenURL:[NSURL URLWithString:@"iosamap://"] 判斷不明白為什麼為否。
- {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- }else{
- [[[UIAlertView alloc]initWithTitle:@"沒有安裝高德地圖" message:@"" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil] show];
- }
- }
1,App 調起系統自帶高德地圖路線規劃進行導航。
- #pragma mark ------------------------------ 導航 - iosMap
- -(void) onDaoHangForIOSMap
- {
- //起點
- CLL