国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS實現設備判斷是否安裝相關地圖(百度、高德等)

2019-10-21 18:42:18
字體:
來源:轉載
供稿:網友

前言

最近項目關于地圖的,和朋友一起做的,他們用的高德地圖,他做到半路有事,我來接手,結果我手機上沒有安裝高德地圖,到我這邊點擊導航沒啥反應,后來就查了一下,簡單處理下,最終實現以下的需求:

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

具體做法如下:

1、plist文件進行相關的配置

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

常見的地圖對應如下:

  • 百度地圖:baidumap
  • 高德地圖:ios/116540.html">iosamap
  • 谷歌地圖:comgooglemaps
  • 騰訊地圖:qqmap

ios,判斷app是否安裝,判斷應用是否安裝,ios判斷有沒有安裝app

你也可以直接直接復制以下代碼到plist文件

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

2.使用系統的API判斷設備是否安裝相關的地圖應用程序

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

具體寫發如下:

百度地圖

 [[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值即可判斷該設備有沒有安裝相關的地圖應用

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

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

-(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",@"導航功能",@"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",@"導航測試",@"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 *  //蘋果原生地圖方法  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; //終點坐標 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]; }

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

import <MapKit/MapKit.h>...

備注:

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

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

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 越西县| 松溪县| 鹤峰县| 广安市| 宜宾市| 沛县| 武隆县| 巧家县| 天门市| 若羌县| 兴城市| 常宁市| 贵溪市| 房产| 肃南| 汶上县| 上栗县| 乌兰浩特市| 纳雍县| 阿合奇县| 凌海市| 南昌县| 霸州市| 高陵县| 正镶白旗| 乌兰察布市| 西华县| 常熟市| 望谟县| 南平市| 舟曲县| 平罗县| 宁国市| 饶平县| 大关县| 昂仁县| 永兴县| 芜湖市| 新建县| 濮阳市| 屏南县|