#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
iOS 獲取導(dǎo)航路線信息(MapKit框架)
最后編輯于 :
?著作權(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ù)。
【社區(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)容
- 獲取導(dǎo)航路線信息 1. 實(shí)現(xiàn)須知 獲取導(dǎo)航路線, 需要向蘋果服務(wù)器發(fā)送網(wǎng)絡(luò)請(qǐng)求 記住關(guān)鍵對(duì)象MKDirection...
- /** @title: iOS利用HealthKit框架從健康app中獲取步數(shù)信息 1.第一步首先需要開啟Heal...
- 【視也導(dǎo)讀】中國的物流75%是通過公路物流來運(yùn)輸,相比鐵路、海運(yùn)、空港運(yùn)輸,公路運(yùn)輸還比較落后,仍然需要靠個(gè)體無序...
- 今天在看人家一個(gè)繼承ImagView的自定義控件,發(fā)現(xiàn)他覆寫了這下面這四個(gè)方法。就在網(wǎng)上找了下這四個(gè)方法有啥區(qū)別,...
- 文/紐森獵頭 王凡 獵頭關(guān)注的是是否可以解決企業(yè)的人才問題,費(fèi)率不是獵頭第一要義關(guān)注的 今天我又接到一位HR朋友的...