#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. Linux 设置系统时间和日期 API

    嵌入式Linux 设置时间和日期 API ,它是busybox要提取的源代码. Linux设置时间和日期的步骤: 1. 设置系统时间和日期: 2. 该系统的时间和日期,同步到硬件. #include ...

  2. java中最简单的方式新起一个线程

    启动一个线程在一个方法中启动一个线程,有两种方法第一种是让类实现Runable接口,这样的话编译器就会提示你实现里面的未实现的方法(就是run方法)第二种是,现在方法中new一个线程,然后直接调用他的 ...

  3. Xcode 4-PBXcp error修复-No such file or directory

    Xcode 4-PBXcp error修复-No such file or directory (2013-05-02 15:20:50) 转载▼ 标签: apple iphone ios 移动开发 ...

  4. 如何在mysql命令窗口获取到程序正在执行的sql语句

    步骤: 1.进入mysql的命令窗口: 2.运行use information_schema; 3.运行select * from PROCESSLIST where info is not null ...

  5. c语言,strcat(),字符串拼接

    #include<stdio.h> #include<string.h> int main() {  char destination[25];  char  *zhang=& ...

  6. [置顶] linux第二天,g++,gcc,ps,cat,sort,grep,kill,less,ls -l ,

    33.less sample.txt 分页输出文件内容到屏幕 34./search content (搜索内容) 可以将文档中有searchcontent 的行输出到屏幕 35.grep scienc ...

  7. 解决 kindle 书籍字体颜色偏淡问题的方法

    现象 通过Markdown转换而来的mobi格式书籍都有一个大问题:字体偏淡,放在kindle上看对比度很差. 原因分析: 导致这种问题的原因,可能是因为在制作电子书的过程中,这些内容是被标注了彩色或 ...

  8. Java RMI详解

    RMI:远程方法调用(Remote Method Invocation).能够让在某个java虚拟机上的对象像调用本地对象一样调用另一个java 虚拟机中的对象上的方法. RMI远程调用步骤: 1,客 ...

  9. sbit命令行中运行scala脚本

    一般sbit编译器采成了scala运行工具.启动sbit命令行,输入console,命令行自动切换到scala编辑器面. scala>:paste 然后手动将XXX.scala中的代码拷贝到界面 ...

  10. Oracle错误代码案例总结及解决方案

    引自:http://blog.sina.com.cn/s/blog_9daa54ec0100yr7v.html 常见错误: ORA-00001:违反唯一约束条件(主键错误) ORA-00028:无法连 ...