#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. Mac OS X 10.7下找不到~/Library/Application Support的解决方案

    28二 最近有台机器升级到了Mac OS X 10.7,Finder的sidebar变了不说,连用户目录下的Library目录也不见了.但是Terminal中是有的,估计是被隐藏了.直接在Finder ...

  2. ecto注册码

    First name: Good Last name: Life Serial: ecto_at585-RP00-MP3F-VB8R-L82N-N0CC   First Name: The Last ...

  3. linux 定时任务计划

    crond: unrecognized service 无crond解决办法 安装计划任务:yum -y install vixie-cron

  4. Android开发之线程池使用总结

    线程池算是Android开发中非常常用的一个东西了,只要涉及到线程的地方,大多数情况下都会涉及到线程池.Android开发中线程池的使用和Java中线程池的使用基本一致.那么今天我想来总结一下Andr ...

  5. MySQL的链接,查看数据库,使用数据库,查看表

    MySQL的链接,查看数据库,使用数据库,查看表 mysql> show databases; +--------------------+ | Database | +------------ ...

  6. Ajax编程技术

    AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML. 指一种创建交互式网页应用的网页开发技术. 不是指一种单一的技术,而是有机地利 ...

  7. PHP和MYSQL的编码问题

    http://blog.csdn.net/martinkro/article/details/5352474 1 MYSQL中的字符集概念  Mysql的字符集里有两个概念,一个是"Char ...

  8. windows2008 x86 安装 32位oracle

    1.windows 2008 升级到sp2补丁 下载地址 : http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=15278 2. ...

  9. JS数值输入控制

    在html文本框录入数值时,可用如下方法进行控制判断. 整数:<input type="text" name="aaa" onkeypress=" ...

  10. 2014年12月20日00:33:14-遮罩+进度条-extjs form.isvalid

    1.Extjs : 遮罩+进度条 2.Extjs: extjs form.isvalid http://stackoverflow.com/questions/19354433/extjs-form- ...