移動開發經常用到基於位置的一些導航功能,但是對於對導航功能依賴性不強的的應用,我們直接采用應用外跳轉地圖APP即可。

但是應用間跳轉,首先需要設置白名單,
在iOS 9 下涉及到平台客戶端跳轉,系統會自動到項目info.plist下檢測是否設置平台Scheme,對於需要配置的平台,如果沒有配置,將無法正常跳轉平台客戶端,因此需要配置Scheme名單。本文我們需要添加百度地圖和高德地圖的scheme白名單。
具體方法:在項目的info.plist中添加LSApplicationQueriesSchemes字段,類型是Array,然后添加兩個Item。

下面直接上主要代碼,網上高德都是直接導航的,為啥?都那么做?
- (void)mapBtnclick{
if (![NSString isNotEmptyString:_currentadress]) {
[self.locationManager startUpdatingLocation];
[SDIndicator showInfoWithMessage:@"正在定位,請稍候..."];
}
_actionSheet= [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil];
NSMutableArray *mapsArray=[[NSMutableArray alloc] initWithCapacity:0];
_mapsUrlArray=[[NSMutableArray alloc] init];
NSURL * apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
if ([[UIApplication sharedApplication] canOpenURL:apple_App]) {
[mapsArray addObject:@"使用蘋果自帶地圖導航"];
NSString *urlString=[NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",_strLatitude,_strLongitude,[_model.last_latitude floatValue],[_model.last_longitude floatValue] ];
[_mapsUrlArray addObject:urlString];
}
NSURL * baidu_App = [NSURL URLWithString:@"baidumap://"];
if ([[UIApplication sharedApplication] canOpenURL:baidu_App]) {
[mapsArray addObject:@"使用百度地圖導航"];
NSString *stringURL =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=gcj02",[_model.last_latitude floatValue],[_model.last_longitude floatValue],_model.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[_mapsUrlArray addObject:stringURL];
}
NSURL * gaode_App = [NSURL URLWithString:@"iosamap://"];
if ([[UIApplication sharedApplication] canOpenURL:gaode_App]) {
[mapsArray addObject:@"使用高德地圖導航"];
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&slat=%f&slon=%f&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&t=0",@"龍巔魚鄰",_strLatitude,_strLongitude,_currentadress,[_model.last_latitude floatValue],[_model.last_longitude floatValue],_model.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[_mapsUrlArray addObject:urlString];
}
for (int x=0; x<mapsArray.count; x++) {
[_actionSheet addButtonWithTitle:[mapsArray objectAtIndex:x]];
}
if (_mapsUrlArray.count>0) {
[_actionSheet showInView:self.view.window];
}else{
[SDIndicator showInfoWithMessage:@"建議您安裝高德或者百度地圖"];
}
}
下面說一下,主要的知識點
【1】
使用canOpenURL方法來檢測該手機是否安裝相應APP
該方法會返回一個BOOL值,當為YES時,表明已安裝該APP
【2】
1、蘋果自帶地圖(不需要檢測,所以不需要URL Scheme)
2、百度地圖 baidumap://
3、高德地圖 iosamap://
當然要攜帶參數的話,就按照各個地圖的規則進行傳值即可
ByZqk
