iOS 开发笔记 - 导航到地图
2018-02-21 21:24
411 查看
导航到地图,已经不是什么新鲜事了。网上有好多参考的资料,我总结出只需要两步
第一步:在info中加上支持的各平台
比如:iosamap高德地图、comgooglemaps谷歌地图、baidumap百度地图、qqmap腾讯地图
第二步:直接在使用的地方,调用下面的代码即可
第一步:在info中加上支持的各平台
比如:iosamap高德地图、comgooglemaps谷歌地图、baidumap百度地图、qqmap腾讯地图
<key>LSApplicationQueriesSchemes</key> <array> <string>iosamap</string> <string>comgooglemaps</string> <string>baidumap</string> <string>qqmap</string> </array>
第二步:直接在使用的地方,调用下面的代码即可
- (void)mapChooose { //系统版本高于8.0,使用UIAlertController UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"导航到设备" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; //自带地图 [alertController addAction:[UIAlertAction actionWithTitle:@"自带地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"alertController -- 自带地图"); //使用自带地图导航 MKMapItem *currentLocation =[MKMapItem mapItemForCurrentLocation]; MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:self.coordinate addressDictionary:nil]]; [MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}]; }]]; //判断是否安装了高德地图,如果安装了高德地图,则使用高德地图导航 if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) { [alertController addAction:[UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"alertController -- 高德地图"); NSString *urlsting =[[NSString stringWithFormat:@"iosamap://navi?sourceApplication= &backScheme= &lat=%f&lon=%f&dev=0&style=2",self.coordinate.latitude,self.coordinate.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlsting]]; }]]; } //判断是否安装了百度地图,如果安装了百度地图,则使用百度地图导航 if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) { [alertController addAction:[UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"alertController -- 百度地图"); NSString *urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",self.coordinate.latitude,self.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]]; }]]; } //判断是否安装了谷歌地图,如果安装了谷歌地图,则使用谷歌地图导航 if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) { [alertController addAction:[UIAlertAction actionWithTitle:@"谷歌地图"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving", @"",//appName, @"",//urlScheme, self.coordinate.latitude, self.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; }]]; } //添加取消选项 [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { [alertController dismissViewControllerAnimated:YES completion:nil]; }]]; //显示alertController [self presentViewController:alertController animated:YES completion:nil]; }
相关文章推荐
- IOS开发笔记之六——ios 8地图定位服务问题
- iOS 开发调用系统地图导航
- iOS开发----调用地图导航
- ios Map 开发笔记 四 导航画线
- iOS开发学习笔记——导航控制器1(UINavigationController)
- iOS开发----地图与导航--定位和位置信息获取
- iOS原生地图开发进阶——使用导航和附近兴趣点检索
- IOS 开发笔记——自定义导航栏返回键后如何恢复系统默认返回手势
- iOS开发 - iOS实现应用内打开第三方地图app进行导航
- iOS原生地图开发进阶——使用导航和附近兴趣点检索
- ios开发中如何调用苹果自带地图导航
- iOS开发笔记--调用iPhone的短信、地图、电话、邮件、浏览器
- iOS开发:采用URI方式跳转到各类地图进行导航
- ios开发中如何调用苹果自带地图导航
- iOS开发笔记--调用iPhone的短信、地图、电话、邮件、浏览器
- iOS开发基础学习笔记——导航控制器2->导航栏(UINavigationBar)/工具栏(toolBar)/导航元素按钮(UINavigationItem)
- iOS开发—跳转方式实现地图导航功能
- IOS开发之地图导航
- ios开发中如何调用苹果自带地图导航
- IOS开发学习笔记(导航控制器,视图控制器,模态)