#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. 监控mysql索引使用效率的脚本

      SELECT  t.table_schema AS db,  t.table_name   AS tab_name,  s.index_name   AS index_name,  s.colum ...

  2. JSTL-core核心代码标签库中的forEach,remove, forTokens,choose,when,otherwise,redirect 标签

    <%@ page language="java" import="java.util.*, cn.hncu.domain.*" pageEncoding= ...

  3. windows 下 node 多版本管理工具 - gnvm

    最近写了各个构建工具, 开发环境为mac,需要在windows下测试通过: 因为很久不用windows,windows下的node 版本还是 0.10.* 的,因此决定升级node mac 下我使用的 ...

  4. mongodb环境部署

    mongodb 环境部署 1.环境说明 安装软件:mongodb-linux-x86_64-.tgz 软件安装位置:/usr/local/mongodb 数据存放位置:/var/mongodb/dat ...

  5. ZooKeeper应用场景

    一.数据发布与订阅(配置中心) 发布与订阅模型,即所谓的配置中心,顾名思义就是发布者将数据发布到ZK节点上,供订阅者动态获取数据,实现配置信息的集中式管理和动态更新.例如全局的配置信息,服务式服务框架 ...

  6. C#扫盲之:带你掌握C#的扩展方法、以及探讨扩展方法的本质、注意事项

    1.为什么需要扩展方法 .NET3.5给我们提供了扩展方法的概念,它的功能是在不修改要添加类型的原有结构时,允许你为类或结构添加新方法. 思考:那么究竟为什么需要扩展方法呢,为什么不直接修改原有类型呢 ...

  7. linux下如何删除文件夹

    直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目录名字-r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思删除文件夹实例: rm -rf /var/l ...

  8. ScriptManager的用法

    资料中如实是说:       1, ScriptManager(脚本控制器)是asp.net ajax存在的基础. 2, 一个页面只允许有一个ScriptManager,并且放在其他ajax控件的前面 ...

  9. 20160322 javaweb 学习笔记--response 重定向

    //一般方法 response.setStatus(302); response.setHeader("Location", "/20160314/index.jsp&q ...

  10. virtualbox安装ubuntu出现“The system is running in low-graphics mode”

    cd /etc/X11 sudo mv xorg.conf.failsafe xorg.conf sudo reboot 即可.