导航地图7_路线规划

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/CharlesYaoxin/article/details/48832765
import "ViewController.h"
#import <MapKit/MapKit.h>

@interface ViewController () <MKMapViewDelegate>

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

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self drawLines];
}

#pragma mark - 懒加载

- (MKMapView *)mapView {
    
    if (_mapView == nil) {
        MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
        // 保存经纬度对象
        CLLocationCoordinate2D coordinate2D;
        // 纬度
        coordinate2D.latitude = 39.90;
        // 经度
        coordinate2D.longitude = 116.39;
        MKCoordinateSpan span = MKCoordinateSpanMake(0.01f, 0.01f);
        [mapView setRegion:MKCoordinateRegionMake(coordinate2D, span)];
        [self.view addSubview:mapView];
        mapView.delegate = self;
        _mapView = mapView;
    }
    return _mapView;
}

#pragma mark - 绘制路线  起点 和 终点
/**
 *  绘制路线 输入起点 和 目标点 给你路线 需要地理反编码等
 */
- (void)drawLines {
    
    /**
     1. 创建路线请求
     2. 创建路线
     3. 计算距离
     4. 标注关键节点
     */
    
    // 路线请求类
    MKDirectionsRequest *dirRequest = [[MKDirectionsRequest alloc] init];
    
    // 起点
    CLLocationCoordinate2D srcCoordinate = {39.9087607478,116.3975780499};
    MKPlacemark *srcPlacemark = [[MKPlacemark alloc] initWithCoordinate:srcCoordinate addressDictionary:nil];
    MKMapItem *srcItem = [[MKMapItem alloc] initWithPlacemark:srcPlacemark];
    dirRequest.source = srcItem;
    
    // 终点
    CLLocationCoordinate2D desCoordinate = {40.9087607478,116.3975780499};
    MKPlacemark *desPlacemark = [[MKPlacemark alloc] initWithCoordinate:desCoordinate addressDictionary:nil];
    MKMapItem *desItem = [[MKMapItem alloc] initWithPlacemark:desPlacemark];
    dirRequest.destination = desItem;
    
    __block NSInteger sumDistance = 0;
    // 发送请求
    MKDirections *directions = [[MKDirections alloc] initWithRequest:dirRequest];
    
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        
        if (error == nil) {
            // 取一条路线
            MKRoute *route = response.routes[0];
            // 路线的关键节点(详细信息)
            for (MKRouteStep *step in route.steps) {
                
                // 01. 添加关键标注
                MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc] init];
                pointAnno.coordinate = step.polyline.coordinate; // polyline detailed step geometry
                pointAnno.title = step.polyline.title;
                pointAnno.subtitle = step.polyline.subtitle;
                [self.mapView addAnnotation:pointAnno];
                
                // 02. 添加覆盖物(路线)
                [self.mapView addOverlay:step.polyline];// -> 代理方法
                sumDistance += step.distance;
            }
            NSLog(@"%ld", sumDistance);
        }
    }];
}

#pragma mark - MKMapView 代理方法

// MKAnnotation->  MKAnnotationVie
// MKOverlay   ->  MKOverlayRenderer

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    // 绘制覆盖物
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *lineRenderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        lineRenderer.strokeColor = [UIColor blueColor];
        lineRenderer.lineWidth = 5.0f;
        return lineRenderer;
    }
    return nil;
}

#pragma mark - 跑步软件简单画线

- (void)test {
    
    CLLocationCoordinate2D firstCoordinate = {1,1};
    CLLocationCoordinate2D secondCoordinate = {2,2};
    CLLocationCoordinate2D coordinate[2] = {firstCoordinate,secondCoordinate};
    /**
     *  参数1:经纬度数组的首地址
     *  参数2:经纬度的数量
     */
    MKPolyline *line = [MKPolyline polylineWithCoordinates:coordinate count:2];
    [self.mapView addOverlay:line];
}



@end

展开阅读全文

没有更多推荐了,返回首页