最后更新時間:2017-08-21
前言
- App內(nèi)根據(jù)手機上裝載的地圖App將其顯示在彈出的選擇框改橘,選擇對應(yīng)地圖跳轉(zhuǎn)進入地圖導(dǎo)航滋尉。需要用到
- (BOOL)canOpenURL:(NSURL *)url NS_AVAILABLE_IOS(3_0);
方法判斷手機是否已安裝相應(yīng)地圖App。 - 要進行跳轉(zhuǎn)需要先在xcode的plist文件內(nèi)將目標(biāo)App的url Scheme加入白名單(LSApplicationQueriesSchemes)飞主。
常見第三方地圖App的url Scheme
- 百度地圖:baidumap
- 高德地圖:iosamap
- 谷歌地圖:comgooglemaps
- 騰訊地圖:qqmap
plist白名單設(shè)置
plist文件新增LSApplicationQueriesSchemes
關(guān)鍵字狮惜,類型為NSArray,并在其下添加子目錄既棺,類型為NSString讽挟,內(nèi)容為各地圖對應(yīng)的url Scheme。
代碼示例
//導(dǎo)航只需要目的地經(jīng)緯度丸冕,endLocation為緯度耽梅、經(jīng)度的數(shù)組
-(void)doNavigationWithEndLocation:(NSArray *)endLocation
{
//NSArray * endLocation = [NSArray arrayWithObjects:@"26.08",@"119.28", nil];
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 navAppleMap];
}];
[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];
}
[self presentViewController:alert animated:YES completion:nil];
}
//蘋果地圖
- (void)navAppleMap
{
// CLLocationCoordinate2D gps = [JZLocationConverter bd09ToWgs84:self.destinationCoordinate2D];
//終點坐標(biāo)
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(26.08, 119.28);
//用戶位置
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];
}
附錄-鏈接
參考鏈接:IOS實現(xiàn)應(yīng)用內(nèi)打開第三方地圖app進行導(dǎo)航
相關(guān)鏈接:iOS 跳轉(zhuǎn)方式實現(xiàn)地圖導(dǎo)航功能
相關(guān)鏈接:Xcode常用設(shè)置
相關(guān)鏈接:彈窗