iOS實(shí)現(xiàn)設(shè)備判斷是否安裝相關(guān)地圖(百度、高德等)
前言
最近項(xiàng)目關(guān)于地圖的,和朋友一起做的,他們用的高德地圖,他做到半路有事,我來接手,結(jié)果我手機(jī)上沒有安裝高德地圖,到我這邊點(diǎn)擊導(dǎo)航?jīng)]啥反應(yīng),后來就查了一下,簡(jiǎn)單處理下,最終實(shí)現(xiàn)以下的需求:
點(diǎn)擊導(dǎo)航,底部彈框,顯示用戶設(shè)備上所有的地圖(一般就蘋果自帶的地圖、百度地圖、高德地圖,當(dāng)然了還有其他地圖,個(gè)人感覺就這幾個(gè)用的人比較多,其他的其實(shí)也類似),下面話不多說了,來一起看看詳細(xì)的介紹吧。
具體做法如下:
1、plist文件進(jìn)行相關(guān)的配置
LSApplicationQueriesSchemes (這個(gè)一定不要寫錯(cuò),一定不要寫錯(cuò),一定不要寫錯(cuò),這個(gè)我是有教訓(xùn)的,說多了都是淚)這是一個(gè)數(shù)組,可以添加各地圖的相關(guān)url Scheme
常見的地圖對(duì)應(yīng)如下:
- 百度地圖:baidumap
- 高德地圖:iosamap
- 谷歌地圖:comgooglemaps
- 騰訊地圖:qqmap

你也可以直接直接復(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)用
備注:蘋果自帶的地圖是不需要判斷的
這里貼一段代碼,需要的時(shí)候稍微修改下即可
-(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)航測(cè)試",@"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=終點(diǎn)&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;
//終點(diǎn)坐標(biāo)
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(lat, lon);
//用戶位置
MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
//終點(diǎn)位置
MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:loc addressDictionary:nil] ];
NSArray *items = @[currentLoc,toLocation];
//第一個(gè)
NSDictionary *dic = @{ MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard), MKLaunchOptionsShowsTrafficKey : @(YES)
};
//第二個(gè),都可以用
// NSDictionary * dic = @{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
// MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]};
[MKMapItem openMapsWithItems:items launchOptions:dic];
}
使用記得導(dǎo)入需要的頭文件,比如蘋果自帶地圖
import <MapKit/MapKit.h> ...
備注:
-(void)doNavigationWithEndLocation:(NSArray *)endLocation;該方法中的數(shù)組傳的其實(shí)就是經(jīng)緯度,到時(shí)候根據(jù)自己的需求修改下就可以直接使用
基本的使用就只這樣,希望可以幫到有需求的小伙伴。。。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
IOS 聊天界面(自適應(yīng)文字)的實(shí)現(xiàn)
本文主要介紹一個(gè)實(shí)現(xiàn)聊天界面的思路過程,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
iOS UILabel根據(jù)內(nèi)容自動(dòng)調(diào)整高度
這篇文章主要為大家詳細(xì)介紹了iOS UILabel根據(jù)內(nèi)容自動(dòng)調(diào)整高度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
iOS開發(fā)中runtime常用的幾種方法示例總結(jié)
Runtime也就是所謂的“運(yùn)行時(shí)”,因?yàn)槭窃谶\(yùn)行時(shí)實(shí)現(xiàn)的。下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)中runtime常用的幾種方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-08-08
iOS 標(biāo)簽Tag列表的實(shí)現(xiàn)代碼
這篇文章主要介紹了本篇文章主要介紹了iOS 標(biāo)簽Tag列表的實(shí)現(xiàn)代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04
iOS系統(tǒng)緩存方面開發(fā)的相關(guān)基礎(chǔ)
這篇文章主要介紹了iOS系統(tǒng)緩存方面開發(fā)的相關(guān)基礎(chǔ),示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10
iOS開發(fā)中對(duì)文件目錄的訪問及管理的基本方法小結(jié)
這篇文章主要介紹了iOS開發(fā)中對(duì)文件目錄的訪問及管理的基本方法小結(jié),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10
iOS 隱藏導(dǎo)航條和狀態(tài)欄實(shí)現(xiàn)方法
這篇文章主要介紹了 iOS隱藏導(dǎo)航條和狀態(tài)欄實(shí)現(xiàn)方法的相關(guān)資料,有時(shí)候根據(jù)需求開發(fā)APP 需要隱藏導(dǎo)航欄和狀態(tài)欄,這里提供了實(shí)現(xiàn)方法需要的朋友可以參考下2016-11-11

