iOS 獲取導(dǎo)航路線信息(MapKit框架)

#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) {
        // 廣州地標(biāo)
        CLPlacemark *gzPL = [placemarks firstObject];
        
        [self.geoC geocodeAddressString:@"上海" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            // 上海地標(biāo)
            CLPlacemark *shPL = [placemarks firstObject];
            
            [self getRouteWithBeginPL:gzPL endPL:shPL];
        }];
    }];
}

- (void)getRouteWithBeginPL:(CLPlacemark *)beginPL endPL:(CLPlacemark *)endPL
{
    // 請(qǐng)求導(dǎo)航路線信息
    // 創(chuàng)建一個(gè)獲取導(dǎo)航路線信息的請(qǐng)求
    MKDirectionsRequest *reqeust = [[MKDirectionsRequest alloc] init];
    // 設(shè)置起點(diǎn)和終點(diǎn)
    CLPlacemark *sourceCLPL = beginPL;
    MKPlacemark *sourcePL = [[MKPlacemark alloc] initWithPlacemark:sourceCLPL];
    MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:sourcePL];
    
    reqeust.source = sourceItem;
    
    // 設(shè)置終點(diǎn)
    // 終點(diǎn)
    CLPlacemark *destCLPL = endPL;
    MKPlacemark *destPL = [[MKPlacemark alloc] initWithPlacemark:destCLPL];
    MKMapItem *destItem = [[MKMapItem alloc] initWithPlacemark:destPL];
    reqeust.destination = destItem;
    
    // 創(chuàng)建一個(gè)請(qǐng)求導(dǎo)航路線的對(duì)象
    MKDirections *directions = [[MKDirections alloc] initWithRequest:reqeust];
    
    // 發(fā)起請(qǐng)求,獲取導(dǎo)航路線
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error)
     {
         // 獲取路線信息成功
         if (error == nil) {
             
             /**
              *  MKDirectionsResponse : 路線響應(yīng)對(duì)象
              *     routes : 所有的路線 <MKRoute 路線對(duì)象>
              */
             /**
              *  MKRoute
              * name : 路線名稱
              * advisoryNotices : 警告提示信息
              * distance : 距離
              * expectedTravelTime : 預(yù)期時(shí)間
              *  transportType : 通過方式
              *  polyline : 幾何路線對(duì)應(yīng)的路線數(shù)據(jù)模型
              * steps : 每一步怎么走
              */
             /**
              *  MKRouteStep 
              * instructions : 行走介紹
              * notice : 警告信息
              * polyline : 每一節(jié)路線對(duì)應(yīng)的數(shù)據(jù)模型
              * distance : 距離
              * transportType : 通過方式
              */
         
             [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);
                 }];
             }];
         }
     }];

}

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容