計(jì)算從一個(gè)位置到另一個(gè)位置的時(shí)間和距離
位置1:漕河涇网持,經(jīng)緯度:(31.0930043380, 121.3221189118)
位置2:上海體育館宜岛,經(jīng)緯度:(31.1814714883, 121.4383290964)
1、生成各自的MapItem
MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(31.0930043380, 121.3221189118) addressDictionary:nil];
MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:sourcePlacemark];
sourceItem.name = @"漕河涇";
MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(31.1814714883, 121.4383290964) addressDictionary:nil];
MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
destinationItem.name = @"上海體育館";
2功舀、創(chuàng)建計(jì)算請(qǐng)求
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = sourceItem;
request.destination = destinationItem;
request.transportType = MKDirectionsTransportTypeWalking;//步行模式
transportType有四種模式:
- MKDirectionsTransportTypeWalking;//步行模式
- MKDirectionsTransportTypeAutomobile//車行模式
- MKDirectionsTransportTypeTransit//公交模式
MKDirectionsRequest可以設(shè)置離開(kāi)時(shí)間和到達(dá)時(shí)間萍倡,處理請(qǐng)求時(shí)會(huì)考慮這些因素,選擇最合適的路徑進(jìn)行計(jì)算辟汰。
3列敲、使用MKDirections對(duì)請(qǐng)求進(jìn)行處理
MKDirections *walkingDirections = [[MKDirections alloc] initWithRequest:request];
[walkingDirections calculateETAWithCompletionHandler:^(MKETAResponse * _Nullable response, NSError * _Nullable error) {
}];
MKETAResponse有以下屬性可以供我們使用:
- expectedTravelTime//持續(xù)的時(shí)長(zhǎng),單位:秒
- distance//距離帖汞,單位:米
- expectedDepartureDate//出發(fā)時(shí)間
- expectedArrivalDate//預(yù)計(jì)到達(dá)時(shí)間
2戴而、在官方地圖應(yīng)用中展示兩點(diǎn)間最佳路徑及導(dǎo)航
[MKMapItem openMapsWithItems:@[sourceItem, destinationItem] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeWalking}];
MKLaunchOptionsDirectionsModeKey有三種選擇:
- MKLaunchOptionsDirectionsModeWalking//步行路線
- MKLaunchOptionsDirectionsModeDriving//車行路線
- MKLaunchOptionsDirectionsModeTransit//公交路線