iOS开发----调用地图导航
注意:本文章下的代码有个别变量未知,所以是不能直接跑通的,我也是转别人的
在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];
- }
- }
- 原文:http://blog.csdn.net/u011011341/article/details/9233049#
iOS开发----调用地图导航的更多相关文章
- IOS开发之地图导航
一.问题描述 现在很多的APP 都开始引入了地图和定位功能,包括一些餐饮业,团购等.他们都过定位和地图来让用户更加方便的根据自己的位置找到合适的目标,也就是说,现在地图定位已经不再是导航工具类,地图工 ...
- IOS开发UI篇—导航控制器属性和基本使用
IOS开发UI篇—导航控制器属性和基本使用 一.导航控制器的一些属性和基本使用 1.把子控制器添加到导航控制器中的四种方法 (1) 1.创建一个导航控制器 UINavigationController ...
- iOS 调用地图导航
在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤 #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevic ...
- iOS开发系列--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...
- 转-iOS开发系列--地图与定位
来自: http://www.cnblogs.com/kenshincui/p/4125570.html#autoid-3-4-0 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功 ...
- iOS开发系列--地图与定位总结
现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个陌生的 ...
- IOS开发中设置导航栏主题
/** * 系统在第一次使用这个类的时候调用(1个类只会调用一次) */ + (void)initialize { // 设置导航栏主题 UINavigationBar *navBar = [UINa ...
- iOS开发中地图开发的简单应用
iOS上使用地图比Android要方便,只需要新建一个MKMapView,addSubView即可.这次要实现的效果如下: 有标注(大头针),定位,地图. 1.添加地图 1.1 新一个Single V ...
- IOS 手绘地图导航
手绘地图导航 第三方库 NAMapKit, 1)支持在手绘图上标记.缩放 2)支持在单张图片 3)支持瓦片小图片 思路 前提:美工已经切好手绘图,并告知我们当前的缩放级别. 1)确定好手绘图左上角点在 ...
随机推荐
- 如何使用JS脚本从HTML中分离图片标签与文本,替换文本中指定的内容并加粗(原创)
var html='ddfsdfsdfdsd dfsdfsdffds<img _src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4 ...
- oracle系统表查询
oracle查询用户下的所有表 select * from all_tab_comments -- 查询所有用户的表,视图等select * from user_tab_comments -- 查询本 ...
- u3d shader forge 冰渐冻材质
<ignore_js_op> 分享个自己研究的冰材质渐冻shader可以调节的参数很多,并且带模型顶点偏移,能更加真实模拟冰的凹凸厚度感.(参数过大容易出现模型破损,慎用)shader f ...
- AT91-PWM应用
步骤1: make menuconfig配置内核, 开启PWM输出功能. Device Drivers ---> Misc devices ---> <*>Atmel AT3 ...
- Web开发中的主要概念
一.Web开发中的主要概念1.静态资源:一成不变的.html.js.css2.动态资源:JavaWeb.输出或产生静态资源.(用户用浏览器看到的页面永远都是静态资源) 3.JavaEE:十三种技术的集 ...
- 1044. Shopping in Mars (25)
分析: 考察二分,简单模拟会超时,优化后时间正好,但二分速度快些,注意以下几点: (1):如果一个序列D1 ... Dn,如果我们计算Di到Dj的和, 那么我们可以计算D1到Dj的和sum1,D1到D ...
- C# 字符串替换Replace
C# 中的石strA.Replace(strB,strC)函数可以实现将strA中的strB替换为strC. 但是容易出错的地方是,这并不是就直接替换好了,此函数的返回值才是替换好的字符串,所以还要要 ...
- JS调用BHO
// BHO 中添加下面的函数,设置UIHandler// BHO 包含如下成员变量: // CDocDispatch m_docDispatch;// CComPtr<IDocHostUIHa ...
- 0909 45作业one
1.编译原理学什么? 答: 初遇编译原理,我知道编译原理是计算机专业设置的一门重要的专业课程,主要是介绍编译程序构造的一般原理和基本方法.其内容大概包括语言和文法.词法分析.语法分析.语法制导翻译.中 ...
- 基本套接字编程(3) -- select篇
1. I/O复用 我们学习了I/o复用的基本知识,了解到目前支持I/O复用的系统调用有select.pselect.poll.epoll.而epoll技术以其独特的优势被越来越多的应用到各大企业服务器 ...