MKMapView绘制路线

51 篇文章 0 订阅
#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值