iOS 打开第三方导航
来源:互联网 发布:美工能做到多少岁 编辑:程序博客网 时间:2024/04/30 03:06
在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用来获取手机的系统,判断系统是多少
CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate; CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01); if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下,调用google map NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude]; // @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *aURL = [NSURL URLWithString:urlString]; [[UIApplication sharedApplication] openURL:aURL]; } else { // 直接调用ios自己带的apple map MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation]; MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]]; toLocation.name = @"to name"; [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}]; }
如果不想使用苹果自带的地图的话,也可以使用第三方的地图,如百度、Google Maps、高德等
使用前,先判断设备上是否已安装应用
百度地图:
if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"baidumap://map/"]])
参考
高德地图:
if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"iosamap://"]])
参考
Google Maps:
if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"comgooglemaps://"]])
参考示例代码
(void)availableMapsApps { [self.availableMaps removeAllObjects]; CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate; CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01); NSString *toName = @"to name"; if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){ NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=transit", startCoor.latitude, startCoor.longitude, endCoor.latitude, endCoor.longitude, toName]; NSDictionary *dic = @{@"name": @"百度地图", @"url": urlString}; [self.availableMaps addObject:dic]; } if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) { NSString *urlString = [NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=applicationScheme&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=0&style=3", @"云华时代", endCoor.latitude, endCoor.longitude]; NSDictionary *dic = @{@"name": @"高德地图", @"url": urlString}; [self.availableMaps addObject:dic]; } if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) { NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit", endCoor.latitude, endCoor.longitude, startCoor.latitude, startCoor.longitude]; NSDictionary *dic = @{@"name": @"Google Maps", @"url": urlString}; [self.availableMaps addObject:dic]; } }
显示一个ActionSheet
[self availableMapsApps]; UIActionSheet *action = [[UIActionSheet alloc] init]; [action addButtonWithTitle:@"使用系统自带地图导航"]; for (NSDictionary *dic in self.availableMaps) { [action addButtonWithTitle:[NSString stringWithFormat:@"使用%@导航", dic[@"name"]]]; } [action addButtonWithTitle:@"取消"]; action.cancelButtonIndex = self.availableMaps.count + 1; action.delegate = self; [action showInView:self.view];
实现delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate; CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01); if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下,调用google map NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude]; // @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *aURL = [NSURL URLWithString:urlString]; [[UIApplication sharedApplication] openURL:aURL]; } else{// 直接调用ios自己带的apple map MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation]; MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]; MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:placemark]; toLocation.name = @"to name"; [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}]; } }else if (buttonIndex < self.availableMaps.count+1) { NSDictionary *mapDic = self.availableMaps[buttonIndex-1]; NSString *urlString = mapDic[@"url"]; urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlString]; DEBUG_LOG(@"\n%@\n%@\n%@", mapDic[@"name"], mapDic[@"url"], urlString); [[UIApplication sharedApplication] openURL:url]; } }
0 0
- iOS 打开第三方导航
- iOS 打开第三方应用
- iOS打开第三方应用
- iOS--打开第三方应用
- IOS实现应用内打开第三方地图app进行导航
- IOS实现应用内打开第三方地图app进行导航
- IOS 调用第三方地图APP导航
- ios开发 第三方应用打开模式
- iOS用第三方应用打开文件
- ios中代码打开第三方app
- iOS 打开第三方 APP 配置 参数
- iOS UIDocumentInteractionController 第三方打开详解
- swift iOS9之后,打开第三方地图导航
- swift3.0打开第三方地图APP进行导航
- ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件
- ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件
- iOS app 打开第三方接口、拨打电话、打开地图第三方app等
- ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件
- codeforces567E. President and Roads
- 复制Tomcat注意事项
- Android视频播放之边缓存边播放
- 今天谈谈文学谈谈艺术
- Java中的static关键字解析
- iOS 打开第三方导航
- unity3d图片导入尺寸改变的原因
- RequireJS入门(二)
- Java接口中的成员变量为什么必须是public static final?
- Kaggle 机器学习竞赛冠军及优胜者的源代码汇总
- JMP、Hook
- 三十三张赤裸裸的人性图
- Eclipse导出可运行的jar包并运行
- 互联网推送服务原理:长连接+心跳机制(MQTT协议)