绘制系统地图路线

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

@interface ViewController ()


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


@end

@implementation ViewController


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



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
   [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 systemNavWithBeginPL:gzPL endPL:shPL];
           
           
       }];
       
       
   }];
    

    
  
    
 
}


/**
 *  根据起点和终点地标对象,调用系统APP进行导航
 *
 *  @param beginCLPL 起点地标
 *  @param endCLPL   终点地标
 */
-(void)systemNavWithBeginPL:(CLPlacemark *)beginCLPL endPL : (CLPlacemark *)endCLPL
{
    // 调用系统的APP进行导航

    // 地图起点地标对象
    MKPlacemark *beginPL = [[MKPlacemark alloc] initWithPlacemark:beginCLPL];
    // 起点
    MKMapItem *beginItem = [[MKMapItem alloc] initWithPlacemark:beginPL];
    

    // 地图终点地标对象
    MKPlacemark *endPL = [[MKPlacemark alloc] initWithPlacemark:endCLPL];
    // 终点
    MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endPL];
    
    
    // 起点和终点数组
    NSArray *items = @[beginItem, endItem];
    
    // 设置地图启动项(导航模式:驾驶, 地图类型: 混合, 是否显示交通: 是)
    NSDictionary *dic = @{
                          MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                          MKLaunchOptionsMapTypeKey : @(MKMapTypeHybrid),
                          MKLaunchOptionsShowsTrafficKey : @(YES),
                          
                          };
    
    // 给定两个点,起点和终点, 然后设置启动项, 开始调用系统APP进行导航
    [MKMapItem openMapsWithItems:items launchOptions:dic];
}


@end

  

原文地址:https://www.cnblogs.com/yedayi/p/5143967.html