歡迎訪問我的博客muhlenXi,該文章出自我的博客。
版權聲明:本文為muhlenXi原創文章,轉載請注明出處,未經允許不得轉載.
導語:
在地圖類應用開發中,我們經常有導航這個功能需求。根據導航方式可以分為應用內導航和應用外導航,其中應用內導航指的是使用第三方提供的地圖SDK(高德、百度等)將導航嵌入到我們開發的APP內部。應用外導航指的是以URL Scheme 跳轉的方式,跳轉到對應的地圖APP中,使用對方的導航功能。
本次開發的需求是,實現應用外導航。通過選項列表(UIAlertController
、UIActionSheet
)的方式提供用戶選擇,當用戶既安裝了高德地圖和百度地圖時,則彈出如下圖所示的選項列表。否則用戶安裝了哪個地圖,就增加哪個地圖的選擇項。
環境的配置
在iOS 9 下涉及到平臺客戶端跳轉,系統會自動到項目info.plist下檢測是否設置平臺Scheme,對于需要配置的平臺,如果沒有配置,將無法正常跳轉平臺客戶端,因此需要配置Scheme名單。本文我們需要添加百度地圖和高德地圖的scheme白名單。
具體方法:在項目的info.plist中添加LSApplicationQueriesSchemes
字段,類型是Array,然后添加兩個Item。
如圖:
根據系統的版本號來初始化對應選項列表
我們需要一個屬性來記錄導航目標的終點坐標
@property (nonatomic,assign) CLLocationCoordinate2D coordinate; //!< 要導航的坐標
在viewDidLoad
中初始化該坐標值。
//導航到深圳火車站
self.coordinate = CLLocationCoordinate2DMake(22.53183, 114.117206);
我們再定義一個判斷系統版本是否大于等于8.0的一個宏定義
//系統版本號是否大于8.0
#define IS_SystemVersionGreaterThanEight ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0)
在低于8.0的系統版本中使用UIAlertController會崩潰,所以我們要根據系統版本號來選擇合適的選項列表。
在導航按鈕的事件響應方法中,我們添加如下代碼:
//系統版本高于8.0,使用UIAlertController
if (IS_SystemVersionGreaterThanEight) {
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"導航到設備" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//自帶地圖
[alertController addAction:[UIAlertAction actionWithTitle:@"自帶地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"alertController -- 自帶地圖");
//使用自帶地圖導航
MKMapItem *currentLocation =[MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:self.coordinate addressDictionary:nil]];
[MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}]];
//判斷是否安裝了高德地圖,如果安裝了高德地圖,則使用高德地圖導航
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
[alertController addAction:[UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"alertController -- 高德地圖");
NSString *urlsting =[[NSString stringWithFormat:@"iosamap://navi?sourceApplication= &backScheme= &lat=%f&lon=%f&dev=0&style=2",self.coordinate.latitude,self.coordinate.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlsting]];
}]];
}
//判斷是否安裝了百度地圖,如果安裝了百度地圖,則使用百度地圖導航
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
[alertController addAction:[UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"alertController -- 百度地圖");
NSString *urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",self.coordinate.latitude,self.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];
}]];
}
//添加取消選項
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}]];
//顯示alertController
[self presentViewController:alertController animated:YES completion:nil];
}
else { //系統版本低于8.0,則使用UIActionSheet
UIActionSheet * actionsheet = [[UIActionSheet alloc] initWithTitle:@"導航到設備" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"自帶地圖", nil];
//如果安裝高德地圖,則添加高德地圖選項
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
[actionsheet addButtonWithTitle:@"高德地圖"];
}
//如果安裝百度地圖,則添加百度地圖選項
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
[actionsheet addButtonWithTitle:@"百度地圖"];
}
[actionsheet showInView:self.view];
}
實現UIActionSheetDelegate 代理方法
當使用UIActionSheet
時,需要設置Delegate
為self
,并且遵循UIActionSheetDelegate
協議,實現相應代理方法。
當點擊取消選項時,會觸發該代理方法
#pragma mark - UIActionSheetDelegate
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
NSLog(@"ActionSheet - 取消了");
[actionSheet removeFromSuperview];
}
當點擊其他選項是,則會觸發下面的代理方法
使用自帶地圖導航時,需要用到MKMapItem
,我們需要導入頭文件#import <MapKit/MapKit.h>
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"numberOfButtons == %ld",actionSheet.numberOfButtons);
NSLog(@"buttonIndex == %ld",buttonIndex);
if (buttonIndex == 0) {
NSLog(@"自帶地圖觸發了");
MKMapItem *currentLocation =[MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:self.coordinate addressDictionary:nil]];
[MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}
//既安裝了高德地圖,又安裝了百度地圖
if (actionSheet.numberOfButtons == 4) {
if (buttonIndex == 2) {
NSLog(@"高德地圖觸發了");
NSString *urlsting =[[NSString stringWithFormat:@"iosamap://navi?sourceApplication= &backScheme= &lat=%f&lon=%f&dev=0&style=2",self.coordinate.latitude,self.coordinate.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlsting]];
}
if (buttonIndex == 3) {
NSLog(@"百度地圖觸發了");
NSString *urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",self.coordinate.latitude,self.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];
}
}
//安裝了高德地圖或安裝了百度地圖
if (actionSheet.numberOfButtons == 3) {
if (buttonIndex == 2) {
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSLog(@"只安裝的高德地圖觸發了");
NSString *urlsting =[[NSString stringWithFormat:@"iosamap://navi?sourceApplication= &backScheme= &lat=%f&lon=%f&dev=0&style=2",self.coordinate.latitude,self.coordinate.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlsting]];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
NSLog(@"只安裝的百度地圖觸發了");
NSString *urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",self.coordinate.latitude,self.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];
}
}
}
}
知識點補充
【1】使用canOpenURL
方法來檢測該手機是否安裝相應APP
該方法會返回一個BOOL值,當為YES時,表明已安裝該APP
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]
常用的4個地圖的 URL Scheme:
- 1、蘋果自帶地圖(不需要檢測,所以不需要URL Scheme)
- 2、百度地圖 baidumap://
- 3、高德地圖 iosamap://
- 4、谷歌地圖 comgooglemaps://
【2】wgs84
,gcj-02
,bd-09
是什么?
wgs84
是國際標準,從GPS設備中取出的原始數據就是這個標準的數據,iOS的SDK中用到的坐標系統也是國際標準的坐標系統WGS-84;
gcj-02
是中國標準,行貨GPS設備取出的原始數據是該標準的數據,根據規定,國內出版的各種地圖系統,必須至少采用gcj-02對地理位置進行首次加密。網絡上也稱之為火星坐標。
bd-09
是百度標準,百度SDK使用的就是這個標準。
運行結果圖示
以下是選擇了不同的選項對應的結果圖。
自帶地圖導航。
高德地圖導航。
百度地圖導航。
感謝閱讀,有什么意見可以給我留言!