地图导航划线

蓬宾白
2023-12-01
简单是ios地图划线事例 有上一篇关于地图的注释,着个应该很清楚了
//
//  ViewController.m
//  2014_11_03_地图划线
//
//  Created by Mac10.9 on 14-11-3.
//  Copyright (c) 2014年 xiaoxiaobing. All rights reserved.
//

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

#import "XXBAnnotation.h"

@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate]] >
//为了开启定位服务员
@property (nonatomic, strong) CLLocationManager *locMgr;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLGeocoder *geocoder;
@end

@implementation ViewController

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

/**
 *  懒加载
 */
- (CLLocationManager *)locMgr
{
#warning 定位服务不可用
    if(![CLLocationManager locationServicesEnabled])
    {
        /**
         只有系统的定位功能是关闭的时候才会调用这个方法
         */
        return nil;
    }
   
    if (!_locMgr)
    {
        // 创建定位管理者
        self.locMgr = [[CLLocationManager alloc] init];
        // 设置代理
        self.locMgr.delegate = self;
    }
    return _locMgr;
}

- (void)setupCLLocationManager
{
    // 开始定位用户的位置
    [self.locMgr startUpdatingLocation];
    [self.locMgr requestAlwaysAuthorization];
}
- (CLGeocoder *)geocoder
{
    if (!_geocoder) {
        self.geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
}

- (void)setupMapView
{
    self.mapView.delegate = self;
   
    NSString *address1 = @"北京";
    NSString *address2 = @"广州";
   
    [self.geocoder geocodeAddressString:address1 completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) return;
       
        CLPlacemark *fromPm = [placemarks firstObject];
       
        [self.geocoder geocodeAddressString:address2 completionHandler:^(NSArray *placemarks, NSError *error) {
            if (error) return;
           
            CLPlacemark *toPm = [placemarks firstObject];
           
            [self addLineFrom:fromPm to:toPm];
        }];
    }];
}
/**
 *  添加导航的线路
 *
 *  @param fromPm 起始位置
 *  @param toPm   结束位置
 */
- (void)addLineFrom:(CLPlacemark *)fromPm to:(CLPlacemark *)toPm
{
    // 1.添加2个大头针
    XXBAnnotation *fromAnno = [[XXBAnnotation alloc] init];
    fromAnno.coordinate = fromPm.location.coordinate;
    fromAnno.title = fromPm.name;
    [self.mapView addAnnotation:fromAnno];
   
    XXBAnnotation *toAnno = [[XXBAnnotation alloc] init];
    toAnno.coordinate = toPm.location.coordinate;
    toAnno.title = toPm.name;
    [self.mapView addAnnotation:toAnno];
   
    // 2.查找路线
   
    // 方向请求
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    // 设置起点
    MKPlacemark *sourcePm = [[MKPlacemark alloc] initWithPlacemark:fromPm];
    request.source = [[MKMapItem alloc] initWithPlacemark:sourcePm];
   
    // 设置终点
    MKPlacemark *destinationPm = [[MKPlacemark alloc] initWithPlacemark:toPm];
    request.destination = [[MKMapItem alloc] initWithPlacemark:destinationPm];
   
    // 方向对象
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
   
    // 计算路线
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        NSLog(@"总共%lu条路线", (unsigned long)response.routes.count);
       
        // 遍历所有的路线
        for (MKRoute *route in response.routes) {
            // 添加路线遮盖
            [self.mapView addOverlay:route.polyline];
        }
    }];
}
#pragma mark - MKMapViewDelegate
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    renderer.strokeColor = [UIColor redColor];
    return renderer;
}
@end

 类似资料: