#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLGeocoder *geocoder;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化地理编码对象
self.geocoder = [CLGeocoder new];
//设置代理
self.mapView.delegate = self;
}
//添加遮盖物
- (IBAction)addOverlay:(id)sender {
//1.给定起点和终点
NSString *startPoint = @"北京";
NSString *endPoint = @"深圳";
//2.使用地理编码把名字转成经纬度值
[self.geocoder geocodeAddressString:startPoint completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//取地标数组最后一项(假定服务器返回一个)
CLPlacemark *startPlacemark = [placemarks lastObject];
//3.创建大头针对象,设置属性;添加
MKPointAnnotation *annotation = [MKPointAnnotation new];
annotation.coordinate = startPlacemark.location.coordinate;
annotation.title = @"起点";
[self.mapView addAnnotation:annotation];
//一定写在block内部
[self.geocoder geocodeAddressString:endPoint completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *endPlacemark = [placemarks lastObject];
MKPointAnnotation *annotation = [MKPointAnnotation new];
annotation.coordinate = endPlacemark.location.coordinate;
annotation.title = @"终点";
[self.mapView addAnnotation:annotation];
[self startRoute:startPlacemark withPlacemark:endPlacemark];
}];
}];
//4.画线(添加overlay)
}
- (void)startRoute:(CLPlacemark *)startPlacemark withPlacemark:(CLPlacemark *)endPlacemark {
MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithPlacemark:startPlacemark]];
MKMapItem *targetItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithPlacemark:endPlacemark]];
//创建request对象
MKDirectionsRequest *request = [MKDirectionsRequest new];
//给request设置起点和终点
request.source = sourceItem;
request.destination = targetItem;
//创建MKDirections对象; 发送请求
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
//取出整体路线; 取出对应路线的所有steps;添加polyline
for (MKRoute *route in response.routes) {
NSLog(@"总路程:%f千米; 预计的总时间:%f小时",route.distance/1000, route.expectedTravelTime/3600);
//第一处可以添加几何线
//steps
for (MKRouteStep *step in route.steps) {
NSLog(@"每个step描述:%@; step距离:%f", step.instructions, step.distance);
//第二处可以添加几何线到地图视图上
[self.mapView addOverlay:step.polyline];
}
}
}
}];
}
//设置线的颜色和粗细
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
render.lineWidth = 5.0;
render.strokeColor = [UIColor blueColor];
return render;
}
@end
MKMapView绘制路线
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 作者:Arthur Knopper,原文链接,原文日期:2016-02-29译者:TonyHan;校对:Cryst...
- 一、概述 在实际的开发当中,我们经常会涉及到绘制路径,这里我们总结一下Path的常用API。 二、基本用法 对于一...
- 轨迹纠偏的作用就是去掉绘制路线时候两个定位点之间产生的毛刺和尖角,使路线看起来更加的圆滑,正常 GitHub代码下...