2010-01-20 105 views
3

我需要找到2個位置之間的駕駛距離。我不需要在地圖中顯示路線,只需要計算我在應用程序中需要使用的距離。在iPhone中計算駕駛距離

MapKit是否允許這樣做?有沒有可以使用的替代方案?

我能夠使用CloudMade獲得地理編碼,但似乎沒有獲得駕駛距離的選項。

感謝任何幫助。

回答

3

CloudMade還提供行車路線。如果您只對距離感興趣,請忽略說明。

的API呼叫看起來是這樣的:

http://navigation.cloudmade.com/YOUR-API-KEY-GOES-HERE/api/0.3/47.25976,9.58423,47.26117,9.59882/car/shortest.js

和JSON包括

.... 
route_summary: { 
total_distance: Distance in meters,... 

Source

+0

謝謝你的擡頭。這樣可行。令人沮喪的是,雲計算API正處於如此激烈的發展中,文檔變得容易過時。 此通話是否有v2版本,或者此通話是否將在未來繼續工作? 謝謝。 – Prasanna 2010-01-21 13:11:29

+0

自從去年8月份以來,我們一直使用API​​進行路由,並沒有看到任何變化(除了它在後端變得越來越快)之外。 – FelixLam 2010-01-21 14:18:33

0

在我的應用我用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,你會發現它們有所不同。我正在尋找關於這件事,並且遇到以下鏈接http://www.macworld.co.uk/review/reference-education/apple-maps-vs-google-maps-3464377/

即使蘋果一直在改進他們的地圖服務,他們仍然承認Google(IMO)的準確性,至少在有關駕駛距離的問題上。所以如果你的情況下精度不是非常重要,那麼你可以跟隨蘋果。否則,我會建議檢查Google API。