iOS 绘制导航路线

一、简介

  • 路线也是一个覆盖层
  • 理论指导:在地图上操作覆盖层,其实操作的是覆盖层的数据模型
  • 添加覆盖层:在地图上添加覆盖层数据模型
  • 删除覆盖层:在地图上移除覆盖层数据模型
 - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
MKPolylineRenderer *line;(设置线宽和颜色)

二、基本使用

  • 实现代码如下:
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController ()<MKMapViewDelegate>


/** 地理编码 */
@property (nonatomic, strong) CLGeocoder *geoC;

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

@implementation ViewController
//** 路线也是一个覆盖层 ** MVC
//** 理论指导:在地图上操作覆盖层,其实操作的是覆盖层的数据模型 **
//** 添加覆盖层:在地图上添加覆盖层数据模型 **
//** 删除覆盖层:在地图上移除覆盖层数据模型 **



#pragma mark -懒加载
-(CLGeocoder *)geoC
{
    if (!_geoC) {
        _geoC = [[CLGeocoder alloc] init];
    }
    return _geoC;
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    
    // 测试, 添加圆形覆盖层
    // == 添加圆形覆盖层 数据模型
    
    // 创建一个圆形的覆盖层数据模型
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:self.mapView.centerCoordinate radius:1000000];
    
    [self.mapView addOverlay:circle];

    
//    [self.geoC geocodeAddressString:@"广州" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//        // 广州地标
//        CLPlacemark *gzPL = [placemarks firstObject];
//        
//        [self.geoC geocodeAddressString:@"上海" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//            // 上海地标
//            CLPlacemark *shPL = [placemarks firstObject];
//            
//            [self getRouteWithBeginPL:gzPL endPL:shPL];
//            
//        }];
//    }];
//
}


- (void)getRouteWithBeginPL:(CLPlacemark *)beginPL endPL:(CLPlacemark *)endPL
{
    // 请求导航路线信息
    
    // 创建一个获取导航路线信息的请求
    MKDirectionsRequest *reqeust = [[MKDirectionsRequest alloc] init];
    // 设置起点和终点
    CLPlacemark *sourceCLPL = beginPL;
    MKPlacemark *sourcePL = [[MKPlacemark alloc] initWithPlacemark:sourceCLPL];
    MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:sourcePL];
    
    reqeust.source = sourceItem;
    
    // 设置终点
    // 终点
    CLPlacemark *destCLPL = endPL;
    MKPlacemark *destPL = [[MKPlacemark alloc] initWithPlacemark:destCLPL];
    MKMapItem *destItem = [[MKMapItem alloc] initWithPlacemark:destPL];
    reqeust.destination = destItem;
    
    
    // 创建一个请求导航路线的对象
    MKDirections *directions = [[MKDirections alloc] initWithRequest:reqeust];
    
    // 发起请求,获取导航路线
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error)
     {
         // 获取路线信息成功
         if (error == nil) {
             
             /**
              *  MKDirectionsResponse : 路线响应对象
              *     routes : 所有的路线 <MKRoute 路线对象>
              */
             /**
              *  MKRoute
              * name : 路线名称
              * advisoryNotices : 警告提示信息
              * distance : 距离
              * expectedTravelTime : 预期时间
              *  transportType : 通过方式
              *  polyline : 几何路线对应的路线数据模型
              * steps : 每一步怎么走
              */
             /**
              *  MKRouteStep 
              * instructions : 行走介绍
              * notice : 警告信息
              * polyline : 每一节路线对应的数据模型
              * distance : 距离
              * transportType : 通过方式
              */
         
           
             MKRoute *route = [response.routes firstObject];
             
             // 如果想要添加一个覆盖层(路线==覆盖层), 就直接添加覆盖层对应的数据模型
             
//             [self.mapView addOverlay:route.polyline];
             
             
//             [response.routes enumerateObjectsUsingBlock:^(MKRoute * _Nonnull route, NSUInteger idx, BOOL * _Nonnull stop) {
//                 
//                 NSLog(@"路线名称:%@---距离--%f", route.name, route.distance);
//                 
//                 [route.steps enumerateObjectsUsingBlock:^(MKRouteStep * _Nonnull step, NSUInteger idx, BOOL * _Nonnull stop) {
//                     
//                     NSLog(@"%@", step.instructions);
//    
//                     
//                 }];
//                 
//                 
//                 
//             }];
             
         }
         
     }];
}



#pragma mark -MKMapViewDelegate

/**
 *  当我们添加一个覆盖层数据模型时, 系统就会调用这个方法查找对应的渲染图层
 *
 *  @param mapView 地图
 *  @param overlay 覆盖层数据模型
 *
 *  @return 渲染层
 */
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    
    MKCircleRenderer *render = [[MKCircleRenderer alloc] initWithOverlay:overlay];
    
    render.fillColor = [UIColor greenColor];
    render.alpha = 0.6;
    
    return render;
}

- (void)ADDLine:(id<MKOverlay>)overlay
{
    NSLog(@"获取渲染图层");
    MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    
    // 设置线宽
    render.lineWidth = 3;
    
    // 设置线的颜色
    render.strokeColor = [UIColor redColor];
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,314评论 19 139
  • 跟踪显示用户的位置 设置MKMapView的userTrackingMode属性可以跟踪显示用户的当前位置 MKU...
    JonesCxy阅读 6,488评论 0 4
  • 绘制导航路线 1. 理论支持 路线也是一个覆盖层 在地图上操作覆盖层,其实操作的是覆盖层的数据模型1. 添加覆盖层...
    翻这个墙阅读 4,453评论 0 0
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,429评论 11 349
  • 2017.5.7 周日 晴 亲子日记第二天 今天坐车回家,看到许多老人牵着狗,又看见了许多大人在边玩儿手机边走...
    鑫隆妈妈阅读 1,020评论 0 0