#import "PPViewController.h"

#import <MapKit/MapKit.h>

#import "PPAnnotation.h"

@interface PPViewController ()<MKMapViewDelegate>

/**

*  编码对象

*/

@property (nonatomic, strong) CLGeocoder *geocoder;

@property (weak, nonatomic) MKMapView *mapView;

@end

@implementation PPViewController

// 懒加载

- (CLGeocoder *)geocoder{

if (!_geocoder) {

_geocoder = [[CLGeocoder alloc] init];

}

return _geocoder;

}

- (void)drawRoute:(id)sender {

// 2. 利用GEO对象进行地理编码,获取到地标对象(CLPlacemark)

[self.geocoder geocodeAddressString:self.startStr completionHandler:^(NSArray *placemarks, NSError *error) {

if (placemarks.count == 0) return;

// 开始位置地标

CLPlacemark *startPlacemark = [placemarks firstObject];

[self.geocoder geocodeAddressString:self.endStr completionHandler:^(NSArray *placemarks, NSError *error) {

if (placemarks.count == 0) return;

// 添加两个大头针 - 终点,起点

PPAnnotation *startAnno = [[PPAnnotation alloc] init];

startAnno.title = startPlacemark.locality;

startAnno.subtitle = startPlacemark.name;

startAnno.coordinate = startPlacemark.location.coordinate;

[self.mapView addAnnotation:startAnno];

// 结束位置地标

CLPlacemark *endPlacemark = [placemarks firstObject];

PPAnnotation *endAnno = [[PPAnnotation alloc] init];

endAnno.title = endPlacemark.locality;

endAnno.subtitle = endPlacemark.name;

endAnno.coordinate = endPlacemark.location.coordinate;

[self.mapView addAnnotation:endAnno];

// 3. 再利用获取到的地标对象(CLPlacemark)创建(MKpalcemark) - 起点的item

[self startDirectionsWithStartClPlacemark:startPlacemark endCLPlacemark:endPlacemark];

}];

}];

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

MKMapView *mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];

self.mapView = mapView;

[self.view addSubview:self.mapView];

// 设置代理

self.mapView.delegate = self;

// 设置模式

self.mapView.mapType = MKMapTypeStandard;

// 设置跟踪

self.mapView.userTrackingMode = MKUserTrackingModeFollow;

// 设置xuanzhuan

self.mapView.rotateEnabled = NO;

//

UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];

button.frame = CGRectMake(100, 100, 100, 100);

[self.view addSubview:button];

[button addTarget:self action:@selector(drawRoute:) forControlEvents:UIControlEventTouchUpInside];

}

/**

*  发送请求, 获取路线信息

*

*  @param startCLPlacemark 起点地标

*  @param endClPlacemark   终点地标

*/

- (void)startDirectionsWithStartClPlacemark:(CLPlacemark *)startCLPlacemark endCLPlacemark:(CLPlacemark *)endCLPlacemark

{

// 0. 创建起点对象和终点对象

MKPlacemark *startMKPlacemark = [[MKPlacemark alloc] initWithPlacemark:startCLPlacemark];

MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startMKPlacemark];

MKPlacemark *endMKPlacemark = [[MKPlacemark alloc] initWithPlacemark:endCLPlacemark];

MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endMKPlacemark];

// 1. 发送请求到苹果服务器获取导航路线

// 创建request对象, 说明起点 - 终点

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

request.source = startItem;

request.destination = endItem;

MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

// 2. 根据服务器返回的路线信息, 绘制导航路线

// 计算完成以后, 调用block, 会返回一个response(包含路线信息)

[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

// 2.1 打印返回的路线信息

for (MKRoute *route in response.routes) {

LogRed(@"%f -- %f",route.distance / 1000, route.expectedTravelTime / 3600);

// 2.2 绘制路线 - 往地图上添加遮盖

// 传递当前路线的几何遮盖给地图, 地图就会根据遮盖自动绘制路线

// 当系统开始绘制路线时, 会询问线条宽度和颜色等

[self.mapView addOverlay:route.polyline];

NSArray *array = route.steps;

for (MKRouteStep *step in array) {

LogGreen(@"%@ --  %f",step.instructions, step.distance);

}

}

}];

}

#pragma mark - MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

// 移动地图到当前用户所在位置

[self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];

}

/**

*  绘制路线时会调用(添加遮盖时会调用)

*

*  @param mapView   mapView

*  @param overlay

*/

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay

{

// 创建一条路径遮盖

MKPolylineRenderer*line = [[MKPolylineRenderer alloc] initWithPolyline:overlay];

line.lineWidth = 10; // 路线宽度

line.strokeColor = [UIColor redColor];//路线宽度

return line;

}

导航 -MapKit - 获取路线信息绘制导航路线的更多相关文章

  1. iOS系统导航/自绘制导航路线

    系统自带导航 /** 系统自带导航 当前位置导航到目的地 1.根据目的地进行地理编码 2.把当前位置和目的地封装成MKMapItem对象 3.使用 MKMapItem openMapsWithItem ...

  2. 【iOS】7.4 定位服务->3.2 地图框架MapKit 功能2:路线规划(导航)

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  3. 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop

    [源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...

  4. router.beforeEach、路由元信息、导航守卫与函数式编程

    一.函数的识别: 1.router.beforeEach:主函数.高阶函数.入口函数: 2.匿名参量函数:处理跳转过程中的附加逻辑 (to, from, next) => { if (to.ma ...

  5. Android获取位置信息的方法总结

    1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位.移动通信.导航等多种技术,提供与空间位置相关的综 ...

  6. [iOS微博项目 - 3.4] - 获取用户信息

    github: https://github.com/hellovoidworld/HVWWeibo   A.获取用户信息 1.需求 获取用户信息并储存 把用户昵称显示在“首页”界面导航栏的标题上   ...

  7. PowerShell_零基础自学课程_6_PS中获取帮助信息详解、管道、格式化输

    前些文章陆续的说了一些关于这些主题,但是讨论的都不够深入,今天我们深入的了解一下获取帮助信息.管道以及格式化输出的内容. 一.获取帮助信息 在PS中获取帮助信息,最常用的有: -? .get-comm ...

  8. React Native之获取通讯录信息并实现类通讯录列表(ios android)

    React Native之获取通讯录信息并实现类通讯录列表(ios android) 一,需求分析 1,获取通讯录信息,筛选出通讯录里有多少好友在使用某个应用. 2,获取通讯录信息,实现类通讯录,可拨 ...

  9. Android 获取手机信息,设置权限,申请权限,查询联系人,获取手机定位信息

    Android 获取手机信息,设置权限,申请权限,查询联系人,获取手机定位信息 本文目录: 获取手机信息 设置权限 申请权限 查询联系人 获取手机定位信息 调用高德地图,设置显示2个坐标点的位置,以及 ...

随机推荐

  1. Ajax之旅(一)--什么是Ajax

    本来在学习DRP,但是无意中发现所附资料中有一些參考书籍,当中就有一个关于Ajax的,看了看,挺好的,于是决定暂停一下DRP,再次学习一下Ajax.记得第一遍学习Ajax的时候认为真的是一团雾水,看了 ...

  2. Systemd 入门教程:命令篇

    http://www.ruanyifeng.com/blog/2014/09/illustration-ssl.html

  3. apache+php+mysql最新版windows下

    卸载以前的Apache 1.控制面板先卸载 2.删除E:\Program Files (x86)\Apache Software Foundation下的apache目录 一.安装apache 1.c ...

  4. Java基础知识强化之集合框架笔记73:如何选择使用哪种集合

    1. 到底使用那种集合.    看需求 是否是键值对象形式: 是:Map 键是否需要排序: 是:TreeMap 否:HashMap 不知道,就使用HashMap. 否:Collection 元素是否唯 ...

  5. Java基础知识强化之集合框架笔记70:模拟斗地主洗牌和发牌(ArrayList)

    1. 模拟斗地主洗牌和发牌 分析:     A:创建一个牌盒     B:装牌     C:洗牌     D:发牌     E:看牌 2. 代码实现: package cn.itcast_03; im ...

  6. javascript进击(一)简介

    javascript是属于网络的脚本语言(javascript与java就像老婆与老婆饼,并没有关系) 页面静态效果:HTML+CSS 为页面添加动态效果:javascript JavaScript ...

  7. 构建本地yum源之rpmbuild问题汇总

    - check-rpaths的问题 今天在编译varnish的时候,遇到几个问题,其中一个就是出现如下错误: ...+ /usr/lib/rpm/check-rpaths /usr/lib/rpm/c ...

  8. Nginx高性能服务器安装、配置、运维 (2) —— Nginx安装

    三.Nginx 安装 使用SecureCRT以Root身份登录阿里云,在安装Nginx前先做好阿里云磁盘挂载 -------------- 挂载磁盘 -------------- 1.df -h #显 ...

  9. POJ3974 Palindrome

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  10. jQuery 定时局部刷新(setInterval)方法总结

    来自:http://www.jbxue.com/article/8516.html 1.jQuery 定时局部刷新(setInterval),显示时间的代码. <head> <scr ...