iOS 绘制导航路线

作者: iOS_成才录 | 来源:发表于2015-11-12 19:33 被阅读1281次

一、简介

  • 路线也是一个覆盖层
  • 理论指导:在地图上操作覆盖层,其实操作的是覆盖层的数据模型
  • 添加覆盖层:在地图上添加覆盖层数据模型
  • 删除覆盖层:在地图上移除覆盖层数据模型
 - (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

相关文章

  • iOS 绘制导航路线

    一、简介 路线也是一个覆盖层 理论指导:在地图上操作覆盖层,其实操作的是覆盖层的数据模型 添加覆盖层:在地图上添加...

  • 各大导航URl API 集合

    百度导航IOS API 公交、驾车、导航、步行和骑行导航 调起iOS百度地图,展示指定导航模式下从起点到终点的路线...

  • 绘制导航路线

    绘制导航路线 1. 理论支持 路线也是一个覆盖层 在地图上操作覆盖层,其实操作的是覆盖层的数据模型1. 添加覆盖层...

  • 地图导航

    URI跳转方式地图导航的代码实践iOS调用第三方地图路线导航IOS实现应用内打开第三方地图app进行导航 高德 i...

  • 地图和定位(五)

    一、导航 导航的三种方式: 1、使用系统APP导航 2、发送网络请求给苹果服务器获取导航路线 2.1、获取导航路线...

  • 浅谈iOS进阶路线

    浅谈iOS进阶路线 浅谈iOS进阶路线

  • iOS导航栏使用总结

    iOS导航栏使用总结 iOS导航栏使用总结

  • iOS导航SDK,网页版百度地图

    1,iOS导航SDK使用cocoapods 2,iOS导航SDK默认的导航方式为:驾驶导航,暂时没有发现骑行和步行...

  • iOS股票K线图

    mark:iOS股票K线图 iOS 股票K线图绘制 iOS 股票K线图绘制 从零开始实现k线图走势图绘制(iOS实战篇)

  • 在SwiftUI中使用地图以及生成导航路线

    在学习SwiftUI的过程中涉及到需要使用地图,并且在地图上绘制两点之间的导航路线。发现使用SwiftUI直接自带...

网友评论

  • 5b726ed578ce:你好,不大懂什么是覆盖层,望赐教!

本文标题:iOS 绘制导航路线

本文链接:https://www.haomeiwen.com/subject/xaghhttx.html