MKMapView绘制路线

#import "ViewController.h"
#import 

@interface ViewController ()
@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 * _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 * _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)overlay {
    MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    render.lineWidth = 5.0;
    render.strokeColor = [UIColor blueColor];
    
    return render;
}

@end

你可能感兴趣的:(MKMapView绘制路线)