导航 -MapKit - 获取路线信息绘制导航路线

时间:2021-11-22 04:16:57

#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;

}