系统自带导航

  1. /**
  2. 系统自带导航
  3. 当前位置导航到目的地
  4. 1.根据目的地进行地理编码
  5. 2.把当前位置和目的地封装成MKMapItem对象
  6. 3.使用 MKMapItem openMapsWithItems: launchOptions: 方法进行导航
  7. */
  8. @interface ViewController ()
  9.  
  10. // 目的地的输入框
  11. @property (weak, nonatomic) IBOutlet UITextField *destinationField;
  12.  
  13. /**
  14. * 点击按钮之后开始导航
  15. */
  16. - (IBAction)navigate;
  17.  
  18. @end
  19.  
  20. @implementation ViewController
  21.  
  22. - (IBAction)navigate {
  23. // 1.拿到用户输入的目的地
  24. NSString *destination = self.destinationField.text;
  25. if (destination.length == ) {
  26. return;
  27. }
  28.  
  29. // 2.地理编码
  30. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  31. [geocoder geocodeAddressString:destination completionHandler:^(NSArray *placemarks, NSError *error) {
  32. if (placemarks.count == || error) return ;
  33.  
  34. // 2.1.取出地理编码出的地标
  35. CLPlacemark *clpm = [placemarks firstObject];
  36.  
  37. // 2.2.利用CLPlacemark来创建MKPlacemark
  38. MKPlacemark *mkpm = [[MKPlacemark alloc] initWithPlacemark:clpm];
  39.  
  40. // 2.3.利用MKPlacemark来创建目的地的MKMapItem
  41. MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:mkpm];
  42.  
  43. // 2.4.拿到起点的MKMapItem
  44. MKMapItem *sourceItem = [MKMapItem mapItemForCurrentLocation];
  45.  
  46. // 2.5.开始导航
  47. [self startNavigateWithSourceItem:sourceItem destinationItem:destinationItem];
  48. }];
  49. }
  50.  
  51. /**
  52. * 开始导航
  53. *
  54. * @param sourceItem 起点的Item
  55. * @param destinationItem 终点的Item
  56. */
  57. - (void)startNavigateWithSourceItem:(MKMapItem *)sourceItem destinationItem:(MKMapItem *)destinationItem
  58. {
  59. // 1.将起点和终点item放入数组中
  60. NSArray *items = @[sourceItem, destinationItem];
  61.  
  62. // 2.设置Options参数(字典)
  63. // MKLaunchOptionsDirectionsModeKey :导航模式
  64. // MKLaunchOptionsMapTypeKey:地图类型
  65. // MKLaunchOptionsShowsTrafficKey:是否显示交通状况
  66. NSDictionary *options = @{
  67. MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
  68. MKLaunchOptionsMapTypeKey : @(MKMapTypeHybrid),
  69. MKLaunchOptionsShowsTrafficKey : @YES
  70. };
  71.  
  72. // 3.开始导航
  73. [MKMapItem openMapsWithItems:items launchOptions:options];
  74. }
  75.  
  76. @end

导航自定义绘制路线

  1. #import "ViewController.h"
  2. #import <MapKit/MapKit.h>
  3.  
  4. /**
  5. 根据目的地自实现绘制路线
  6. 1.封装当前位置和目的地为MKMapItem对象
  7. 2.MKDirectionsRequest对象包装源地址和目的地址
  8. 3.MKDirections的对象calculateDirectionsWithCompletionHandler:方法进行绘制
  9. 4.使用Mapview addOverlay:polyLine]添加遍历出来的线路
  10. 6.设置mapview代理,遵守协议,并实现- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(MKPolyline *)overlay方法(当往Mapview中添加一个遮盖到地图上时会执行该方法,此时画线才能正常显示)
  11.  
  12. */
  13. @interface ViewController () <MKMapViewDelegate>
  14.  
  15. // 地图的View
  16. @property (weak, nonatomic) IBOutlet MKMapView *mapView;
  17.  
  18. // 目的地的输入框
  19. @property (weak, nonatomic) IBOutlet UITextField *destinationField;
  20.  
  21. /**
  22. * 点击之后开始画线
  23. */
  24. - (IBAction)drawLine;
  25.  
  26. @end
  27.  
  28. @implementation ViewController
  29.  
  30. - (void)viewDidLoad
  31. {
  32. [super viewDidLoad];
  33.  
  34. self.mapView.delegate = self;
  35. }
  36.  
  37. - (IBAction)drawLine {
  38. // 0.退出键盘
  39. [self.view endEditing:YES];
  40.  
  41. // 1.获取用户输入的目的地
  42. NSString *destination = self.destinationField.text;
  43. if (destination.length == ) {
  44. return;
  45. }
  46.  
  47. // 2.地理编码
  48. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  49. [geocoder geocodeAddressString:destination completionHandler:^(NSArray *placemarks, NSError *error) {
  50. if (placemarks.count == || error) return;
  51.  
  52. // 2.1.获取CLPlaceMark对象
  53. CLPlacemark *clpm = [placemarks firstObject];
  54.  
  55. // 2.2.利用CLPlacemark来创建MKPlacemark
  56. MKPlacemark *mkpm = [[MKPlacemark alloc] initWithPlacemark:clpm];
  57.  
  58. // 2.3.创建目的地的MKMapItem对象
  59. MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:mkpm];
  60.  
  61. // 2.4.起点的MKMapItem
  62. MKMapItem *sourceItem = [MKMapItem mapItemForCurrentLocation];
  63.  
  64. // 2.5.开始画线
  65. [self drawLineWithSourceItem:sourceItem destinationItem:destinationItem];
  66. }];
  67. }
  68.  
  69. /**
  70. * 开始画线
  71. *
  72. * @param sourceItem 起点的Item
  73. * @param destinationItem 终点的Item
  74. */
  75. - (void)drawLineWithSourceItem:(MKMapItem *)sourceItem destinationItem:(MKMapItem *)destinationItem
  76. {
  77. // 1.创建MKDirectionsRequest对象
  78. MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
  79.  
  80. // 1.1.设置起点的Item
  81. request.source = sourceItem;
  82.  
  83. // 1.2.设置终点的Item
  84. request.destination = destinationItem;
  85.  
  86. // 2.创建MKDirections对象
  87. MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
  88.  
  89. // 3.请求/计算(当请求到路线信息的时候会来到该方法)
  90. [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
  91. // 3.1.当有错误,或者路线数量为0直接返回
  92. if (error || response.routes.count == ) return;
  93.  
  94. NSLog(@"%ld", response.routes.count);
  95.  
  96. // 3.2.遍历所有的路线
  97. for (MKRoute *route in response.routes) {
  98.  
  99. // 3.3.取出路线(遵守MKOverlay)
  100. MKPolyline *polyLine = route.polyline;
  101.  
  102. // 3.4.将路线添加到地图上
  103. [self.mapView addOverlay:polyLine];
  104. }
  105. }];
  106. }
  107.  
  108. /**
  109. * 当一个遮盖添加到地图上时会执行该方法
  110. *
  111. * @param overlay 遵守MKOverlay的对象
  112. *
  113. * @return 画线的渲染
  114. */
  115. - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(MKPolyline *)overlay
  116. {
  117. MKPolylineRenderer *poly = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
  118.  
  119. poly.strokeColor = [UIColor yellowColor];
  120. poly.lineWidth = 5.0;
  121.  
  122. return poly;
  123. }
  124.  
  125. @end

iOS系统导航/自绘制导航路线的更多相关文章

  1. 导航 -MapKit - 获取路线信息绘制导航路线

    #import "PPViewController.h" #import <MapKit/MapKit.h> #import "PPAnnotation.h& ...

  2. iOS系统中导航栏的转场解决方案与最佳实践

    背景 目前,开源社区和业界内已经存在一些 iOS 导航栏转场的解决方案,但对于历史包袱沉重的美团 App 而言,这些解决方案并不完美.有的方案不能满足复杂的页面跳转场景,有的方案迁移成本较大,为此我们 ...

  3. 移动产品设计之ios系统的导航

    做道题:[不定项选择题] OS中导航设计模式有几种? A.平铺导航 B.标签导航 C.树形导航 D.模态视图导航 正确答案:A B C 讲解: 导航始终是产品设计的重头戏,往往产品设计中90%的事情就 ...

  4. iOS 超 Easy 实现 渐变导航栏

    接着上周的项目, 在上周我别出心裁的在自定义TabbarController中加入了自定义转场动画, 受到了大家广泛的喜爱, 再次表示感激, 今天我们继续实现LifestyleViewControll ...

  5. iOS用户体验之-导航之道

    iOS用户体验之-导航之道 用户不会意识到有导航指向的存在除非他遇到非预期的效果. 能够说导航时逻辑跳转的节点.所以导航对用户体验是至关重要的. iOS中有三种类型的导航.每一种适合不同类型的app. ...

  6. iOS 上滑隐藏导航,下滑显示导航,仿斗鱼导航效果

    UItableView或 UIcollectionView 都是继承UIScrollView 滑动的时候,判断是上滑还是下滑 使用 UIScrollView 的代理方法 func scrollView ...

  7. 苹果iOS系统下检查第三方APP是否安装及跳转启动

    在iOS系统,使用Url Scheme框架在APP间互相跳转和传递数据,本文只介绍如果检测和跳转. Url Scheme框架 如果你想知道ios设备中是否安装QQ这个软件,我们可以通过一个简单方法判断 ...

  8. iOS: 如何正确的绘制1像素的线

    iOS 绘制1像素的线 一.Point Vs Pixel iOS中当我们使用Quartz,UIKit,CoreAnimation等框架时,所有的坐标系统采用Point来衡量.系统在实际渲染到设置时会帮 ...

  9. iOS系统架构

    1.iOS系统架构 iOS的系统架构分为四个层次 核心操作系统层 (Core OS) 它包括 内存管理 , 文件系统 , 电源管理以及一些其他的操作系统任务, 它可以直接和硬件设备进行交互 核心服务层 ...

随机推荐

  1. iOS 设置铃声---加载音乐和音频然后进行播放

    在有些应用中需要用到背景音乐和音效,那在程序中是这么实现的. 1.首先加载背景音乐需要用到AVFoundation框架 2.音乐资源都是在包里的,所以需要获得包路径,涉及方法- (id)initWit ...

  2. java.lang.IllegalArgumentException: template not initialized; call afterPropertiesSet() before using

    在使用spring-data-redis时使用junit测试报错: java.lang.IllegalArgumentException: template not initialized; call ...

  3. Ajax发送异步请求(四步操作)

    1.第一步(得到XMLHttpRequest) *ajax其实只需要学习一个对象:XMLHttpRequest,如果掌握了它,就掌握了ajax!! *得到XMLHttpRequest >大多数浏 ...

  4. 简化注解shh框架

    找到上次我们搭建的SSH框架 浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="myIndexAction" class=&q ...

  5. css secrets----multiple borders

    原始文档: https://www.zybuluo.com/freeethy/note/193574 box-shadow solution 只能实现solid border box-shadow表现 ...

  6. PS1应用之——修改linux终端命令行各字体颜色

    最近在学习linux操作系统(CentOS 6 & CentOS 7).觉得linux终端命令行全部为白色,会经常导致命令与输出内容难以分辨.于是上网找到修改linux终端命令行颜色的方法,发 ...

  7. PHP中类自动加载的方式

    最近在学习composer,发现从接触PHP到现在已经遇到了三种关于PHP中类的自动加载方式,这其中包括PHP自带的类的自动加载方式.PHP的第三方的依赖管理工具composer的加载方式以及PHP的 ...

  8. python容器类型:列表,字典,集合等

    容器的概念我是从C++的STL中学到的 什么是容器? 容器是用来存储和组织其他对象的对象. 也就是说容器里面可以放很多东西,这些东西可以是字符串,可以是整数,可以是自定义类型,然后把这些东西有组织的存 ...

  9. SpringMVC无法获取请求中的参数的问题的调查与解决(2)

    由于Request的getInputSteam()一旦获取一次后,就再也无法获取了 在实际项目中导致下面的问题: 1,多个拦截器,Filter都需要从InputStream中拿数据的情况无法处理: 2 ...

  10. window10系统安装SQL数据库和小蝴蝶的问题

    最近刚刚升了windows10系统.由于以前一直使用的是SQL2008数据库,所以也就没有下载最新的数据库,但是在安装的过程中一直提示让重启,重启了很多回也没有用. 在启动SQL2008安装程序的时候 ...