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];
- }
- }
iOS 调用地图导航的更多相关文章
- iOS开发----调用地图导航
注意:本文章下的代码有个别变量未知,所以是不能直接跑通的,我也是转别人的 在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤 #define SYSTEM_VERSION_L ...
- ios调用系统导航
#import "ViewController.h" #import <MapKit/MapKit.h> @interface ViewController () @p ...
- iOS调用第三方导航和线路规划
线路规划: https://blog.csdn.net/qq_19979539/article/details/51938995 百度地图:baidumap: 高德地图:iosamap: 腾讯地图:q ...
- ios开发中如何调用苹果自带地图导航
前段时间一直在赶项目,在外包公司工作就是命苦,天天加班不说,工作都是和工期合同挂钩的,稍微逾期就有可能被扣奖金,不谈这些伤脑筋的事情了,让我们说说iOS开发中如何调用苹果手机自带的地图. 学习如逆水行 ...
- iOS原生地图开发进阶——使用导航和附近兴趣点检索
iOS原生地图开发进阶——使用导航和附近兴趣点检索 iOS中的mapKit框架对国际化的支持非常出色.在前些篇博客中,对这个地图框架的基础用法和标注与覆盖物的添加进行了详细的介绍,这篇博客将介绍两个更 ...
- iOS 开发笔记 - 导航到地图
导航到地图,已经不是什么新鲜事了.网上有好多参考的资料,我总结出只需要两步 第一步:在info中加上支持的各平台 比如:iosamap高德地图.comgooglemaps谷歌地图.baidumap百度 ...
- iOS调用第三方地图App进行导航方法
前言 App内根据手机上装载的地图App将其显示在弹出的选择框,选择对应地图跳转进入地图导航.需要用到- (BOOL)canOpenURL:(NSURL *)url NS_AVAILABLE_IOS( ...
- H5调用百度地图导航
template <div class="map"> <div class="content_flex"><img src=&qu ...
- iOS开发之百度地图导航
本篇主要讲述百度地图的导航功能: 第一步:在使用百度导航之前,我们需要在百度地图开放平台上下载导航的 SDK,共85.8M,网速不好的同学可提前准备好. 第二步:引入导航所需的系统包 将AudioTo ...
随机推荐
- Remove Nth Node From End of List [LeetCode]
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- struts2视频学习笔记 01-02
网易云课堂-<struts2> 课时1 Struts2: WebWork2基础上发展而来,MVC框架,无侵入式设计. 提供了拦截器,类型转换器,支持多种表现层技术(JSP, freeMar ...
- java入门第四步之应用服务器的安装(Tomcat)【转】
首先打开myeclipse,在myeclipse菜单栏下面有两个按钮: 左边的按钮是进行项目的部署的(即将项目部署到服务器上),右边的按钮是进行服务器的启动的,如果你安装了tomcat服务器,那你就可 ...
- Maven 系列 二 :Maven 常用命令,手动创建第一个 Maven 项目【转】
1.根据 Maven 的约定,我们在D盘根目录手动创建如下目录及文件结构: 2.打开 pom.xml 文件,添加如下内容: <project xmlns="http://maven.a ...
- [转]BeginInvoke和EndInvoke方法浅析
开发语言:C#3.0 IDE:Visual Studio 2008 一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提 ...
- / etc / init.d / iptables: line 268: restorecon: command not found
When I tried to restart iptables from vps , I got the following error. Iptables encountered such a p ...
- JS中把字符串转成JSON对象的方法
在JS中,把 json 格式的字符串转成JSON对象,关键代码 json = eval('('+str+')'); <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- linux常用的重要的命令: netstat
Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Membershi ...
- shell中的数组
在shell脚本中,除了通常使用的shell变量外,有时也需要复杂的数据结构去实现一些功能,这里简单说明一下shell数组的使用方法: 初始化方法 _array_name[0]="rando ...
- java之通过反射,来获得某对象的所有方法(类方法提取器)
参考Thinging in Java 在编程时, 如果不记得一个类是否有某个方法,或者不知道一个类究竟能做些什么,而又不想通过索引或 类的层次结构去查找jdk文档,这时通过反射的小工具能节省很多时间. ...