[英]Use mapkit to calculate driving distance between two addresses?
是否可以使用iphone sdk中的mapkit來計算兩個地址之間的行車距離?
如何使用Core Location框架:
- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location
你需要兩個地址的緯度/經度。
編輯:在iOS 3.2中不推薦使用。 請改用distanceFromLocation:
方法。
使用iOS7,您可以在API中獲得完整的指示
看一下本教程的教程 。
不,但如果您有經度/緯度,那么計算距離相當容易。 當然這是這些點之間的數學距離。 不是基於實際路線的實際駕駛或步行距離。
在我的應用程序中,我使用MKDirections來獲得兩個位置之間的駕駛(步行)距離。
CLLocationCoordinate2D startCoordinates = YOUR_START_COORDINATES;
CLLocationCoordinate2D endCoordinates = YOUR_END_COORDINATES;
MKPlacemark *startPoint = [[MKPlacemark alloc] initWithCoordinate:startCoordinates];
MKPlacemark *endPoint = [[MKPlacemark alloc] initWithCoordinate:endCoordinates];
MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startPoint];
MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endPoint];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = startItem;
request.destination = endItem;
request.transportType = MKDirectionsTransportTypeAutomobile; //here you can choose a transport type you need
MKDirections *direction = [[MKDirections alloc] initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
if (response) {
for (MKRoute *route in response.routes) {
NSLog(@"Distance : %f", route.distance);
}
}
}];
如果您將位置作為地址,那么您可以使用CLGeocoder的方法,它將為您提供地址的緯度和經度
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
如果您將使用MKDirections的駕駛距離結果與使用Google地圖的駕駛距離結果進行比較,您會發現它們有所不同。 我正在搜索此事並發現以下鏈接http://www.macworld.co.uk/review/reference-education/apple-maps-vs-google-maps-3464377/
盡管蘋果公司一直在改進他們的地圖服務,但他們仍然准確地向谷歌(IMO)承認,至少在有關駕駛距離的問題上。 因此,如果准確性在您的情況下不是非常重要,那么您可以跟隨Apple。 否則,我建議您檢查Google API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.