溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS實現(xiàn)設(shè)備判斷是否安裝相關(guān)地圖(百度、高德等)

發(fā)布時間:2020-10-21 13:35:00 來源:腳本之家 閱讀:319 作者:JustEverOnce 欄目:移動開發(fā)

前言

最近項目關(guān)于地圖的,和朋友一起做的,他們用的高德地圖,他做到半路有事,我來接手,結(jié)果我手機(jī)上沒有安裝高德地圖,到我這邊點擊導(dǎo)航?jīng)]啥反應(yīng),后來就查了一下,簡單處理下,最終實現(xiàn)以下的需求:

點擊導(dǎo)航,底部彈框,顯示用戶設(shè)備上所有的地圖(一般就蘋果自帶的地圖、百度地圖、高德地圖,當(dāng)然了還有其他地圖,個人感覺就這幾個用的人比較多,其他的其實也類似),下面話不多說了,來一起看看詳細(xì)的介紹吧。

具體做法如下:

1、plist文件進(jìn)行相關(guān)的配置

LSApplicationQueriesSchemes (這個一定不要寫錯,一定不要寫錯,一定不要寫錯,這個我是有教訓(xùn)的,說多了都是淚)這是一個數(shù)組,可以添加各地圖的相關(guān)url Scheme

常見的地圖對應(yīng)如下:

  • 百度地圖:baidumap
  • 高德地圖:iosamap
  • 谷歌地圖:comgooglemaps
  • 騰訊地圖:qqmap

iOS實現(xiàn)設(shè)備判斷是否安裝相關(guān)地圖(百度、高德等)

你也可以直接直接復(fù)制以下代碼到plist文件

<key>LSApplicationQueriesSchemes</key>
 <array>
  <string>baidumap</string>
  <string>iosamap</string>
  <string>comgooglemaps</string>
  <string>qqmap</string>
 </array>

2.使用系統(tǒng)的API判斷設(shè)備是否安裝相關(guān)的地圖應(yīng)用程序

- (BOOL)canOpenURL:(NSURL *)url NS_AVAILABLE_IOS(3_0);

具體寫發(fā)如下:

百度地圖

 [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]

高德地圖

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]

谷歌地圖

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]

騰訊地圖

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]

該方法返回的bool值即可判斷該設(shè)備有沒有安裝相關(guān)的地圖應(yīng)用

備注:蘋果自帶的地圖是不需要判斷的

這里貼一段代碼,需要的時候稍微修改下即可

-(void)doNavigationWithEndLocation:(NSArray *)endLocation
{
 NSMutableArray *maps = [NSMutableArray array];
 //蘋果原生地圖-蘋果原生地圖方法和其他不一樣
 NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
 iosMapDic[@"title"] = @"蘋果地圖";
 [maps addObject:iosMapDic];
 //百度地圖
 if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
  NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
  baiduMapDic[@"title"] = @"百度地圖";
  NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%@,%@|name=北京&mode=driving&coord_type=gcj02",endLocation[0],endLocation[1]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  baiduMapDic[@"url"] = urlString;
  [maps addObject:baiduMapDic];
 } 
 //高德地圖
 if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
  NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
  gaodeMapDic[@"title"] = @"高德地圖";
  NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%@&lon=%@&dev=0&style=2",@"導(dǎo)航功能",@"nav123456",endLocation[0],endLocation[1]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  gaodeMapDic[@"url"] = urlString;
  [maps addObject:gaodeMapDic];
 } 
 //谷歌地圖
 if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
  NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];
  googleMapDic[@"title"] = @"谷歌地圖";
  NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%@,%@&directionsmode=driving",@"導(dǎo)航測試",@"nav123456",endLocation[0], endLocation[1]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  googleMapDic[@"url"] = urlString;
  [maps addObject:googleMapDic];
 } 
 //騰訊地圖
 if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
  NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
  qqMapDic[@"title"] = @"騰訊地圖";
  NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%@,%@&to=終點&coord_type=1&policy=0",endLocation[0], endLocation[1]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  qqMapDic[@"url"] = urlString;
  [maps addObject:qqMapDic];
 } 
 //選擇
 UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"選擇地圖" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 
 NSInteger index = maps.count; 
 for (int i = 0; i < index; i++) {  
  NSString * title = maps[i][@"title"];
  //蘋果原生地圖方法
  if (i == 0) {
   UIAlertAction * action = [UIAlertAction actionWithTitle:title style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
    [self navAppleMapnavAppleMapWithArray:endLocation];
   }];
   [alert addAction:action];
   continue;
  }  
  UIAlertAction * action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
   NSString *urlString = maps[i][@"url"];
   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
  }];  
  [alert addAction:action];
 } 
 UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {  
 }]; 
 [alert addAction:action]; 
 [[CPBaseViewController getCurrentVC] presentViewController:alert animated:YES completion:nil];
// [self presentViewController:alert animated:YES completion:nil]; 
}

//蘋果地圖
- (void)navAppleMapnavAppleMapWithArray:(NSArray*) array
{
 float lat = [NSString stringWithFormat:@"%@", array[0]].floatValue;
 float lon = [NSString stringWithFormat:@"%@", array[1]].floatValue;
 //終點坐標(biāo)
 CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(lat, lon); 
 //用戶位置
 MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
 //終點位置
 MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:loc addressDictionary:nil] ]; 
 NSArray *items = @[currentLoc,toLocation];
 //第一個
 NSDictionary *dic = @{       MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,       MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),       MKLaunchOptionsShowsTrafficKey : @(YES)
       };
 //第二個,都可以用
 // NSDictionary * dic = @{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
 //       MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}; 
 [MKMapItem openMapsWithItems:items launchOptions:dic]; 
}

使用記得導(dǎo)入需要的頭文件,比如蘋果自帶地圖

import <MapKit/MapKit.h>
...

備注:

-(void)doNavigationWithEndLocation:(NSArray *)endLocation;該方法中的數(shù)組傳的其實就是經(jīng)緯度,到時候根據(jù)自己的需求修改下就可以直接使用

基本的使用就只這樣,希望可以幫到有需求的小伙伴。。。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI