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

image.png
你也可以直接直接復(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)航測試",@"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ù)自己的需求修改下就可以直接使用
基本的使用就只這樣,希望可以幫到有需求的小伙伴。。。