导航 -MapKit - 获取路线信息绘制导航路线
#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 - 获取路线信息绘制导航路线的更多相关文章
- iOS系统导航/自绘制导航路线
系统自带导航 /** 系统自带导航 当前位置导航到目的地 1.根据目的地进行地理编码 2.把当前位置和目的地封装成MKMapItem对象 3.使用 MKMapItem openMapsWithItem ...
- 【iOS】7.4 定位服务->3.2 地图框架MapKit 功能2:路线规划(导航)
本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...
- 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop
[源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...
- router.beforeEach、路由元信息、导航守卫与函数式编程
一.函数的识别: 1.router.beforeEach:主函数.高阶函数.入口函数: 2.匿名参量函数:处理跳转过程中的附加逻辑 (to, from, next) => { if (to.ma ...
- Android获取位置信息的方法总结
1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位.移动通信.导航等多种技术,提供与空间位置相关的综 ...
- [iOS微博项目 - 3.4] - 获取用户信息
github: https://github.com/hellovoidworld/HVWWeibo A.获取用户信息 1.需求 获取用户信息并储存 把用户昵称显示在“首页”界面导航栏的标题上 ...
- PowerShell_零基础自学课程_6_PS中获取帮助信息详解、管道、格式化输
前些文章陆续的说了一些关于这些主题,但是讨论的都不够深入,今天我们深入的了解一下获取帮助信息.管道以及格式化输出的内容. 一.获取帮助信息 在PS中获取帮助信息,最常用的有: -? .get-comm ...
- React Native之获取通讯录信息并实现类通讯录列表(ios android)
React Native之获取通讯录信息并实现类通讯录列表(ios android) 一,需求分析 1,获取通讯录信息,筛选出通讯录里有多少好友在使用某个应用. 2,获取通讯录信息,实现类通讯录,可拨 ...
- Android 获取手机信息,设置权限,申请权限,查询联系人,获取手机定位信息
Android 获取手机信息,设置权限,申请权限,查询联系人,获取手机定位信息 本文目录: 获取手机信息 设置权限 申请权限 查询联系人 获取手机定位信息 调用高德地图,设置显示2个坐标点的位置,以及 ...
随机推荐
- java17 线程的方法
线程的方法: .isAlive():判断线程是否还活着,即线程是否还未中止. .getPriority():获得线程的优先级数值. .setPriority():设置线程的优先级. .setName( ...
- sed命令详解--转
1.简介 sed是非交互式的编辑器.它不会修改文件,除非使用shell重定向来保存结果.默认情况下,所有的输出行都被打印到屏幕上. sed编辑器逐行处理文件(或输入),并将结果发送到屏幕.具体过程如下 ...
- hibernate----hibernate的基础设置
本次学习的内容是hibernate的基础设置 具体内容为: 一.准备工作 1.新建java工程 2.自动引入相关库(自动生成SessionFactory) 3.将数据库驱动拿进来 4.添加hibern ...
- centos 7.x编写开机启动服务
centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度.关 ...
- R-大数据分析挖掘(5-R基础回顾)
(一)R函数 R是一种解析型语言,输入后可直接获取结果 函数(输入参数,参数) R的函数分为“高级”和“低级函数” • 高级函数可调用低级函数 • 高级函数称为泛型函数 • 函数名 <-‐ ...
- ASP.NET MVC(二) 理解MVC
MVC模型同时提供对HTML.CSS以及JavaScript的完整控制. MVC模型通过三个逻辑层来定义WEB应用程序: (一)Business layer(业务层.模型逻辑) 模型(Model) 模 ...
- word ppt excel文档转换成pdf
1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...
- ios Using CocoaPods to Modularize a Big iOS App->使用CocoaPods来进行模块化开发
Selecting the right architecture for your mobile app is a pretty big deal. It will shape your daily ...
- 原生js的数组除重复
js对数组的操作在平常的项目中也会遇到,除去一些增加,或者减少的操作外,还有一个比较重要的操作就是数组的除重,通过数组的除重,我们可以将一个数组中存在的多个重复的数组进行清理,只留下不重复的.另外下面 ...
- Java Lambda简明教程(一)
Lambda表达式背景 许多热门的编程语言如今都有一个叫做lambda或者闭包的语言特性,包括比较经典的函数式编程语言Lisp,Scheme,也有稍微年轻的语言比如JavaScript,Python, ...