- (BOOL)canOpenURL:(NSURL *)url NS_AVAILABLE_IOS(3_0);
方法判斷手機是否已安裝相應地圖App。plist文件新增LSApplicationQueriesSchemes
關鍵字,類型爲NSArray,並在其下添加子目錄,類型爲NSString,內容爲各地圖對應的url Scheme。ios
白名單LSApplicationQueriesSchemes:數組
//導航只須要目的地經緯度,endLocation爲緯度、經度的數組 -(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",@"導航功能",@"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 * 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]; //終點座標 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實現應用內打開第三方地圖app進行導航xcode