2016-05-31 112 views
0

我試圖計算從一點到另一點的距離。我能找到方法。使用MapKit計算經緯度之間的距離

CLLocationDistance dist = [loc distanceFromLocation:loc2]; 

但它是用直線計算距離。

我想要的是通過道路計算距離。

例如:可以說如果我從地址作爲主頁和地址作爲工作。我想通過公路與家庭工作之間的距離。通過公路我得到750米。但是,如果我通過distanceFromLocation方法計算,我只能得到400米。 有人可以幫助我舉一個例子來計算距離。

+0

請參見本例https://www.shinobicontrols.com/blog/ios7-day-by-day-day-13-route-directions-with-mapkit – Rajesh

+0

最簡單的例子HTTP ://www.devfright.com/mkdirections-tutorial/ – Rajesh

+0

謝謝我現在檢查 – Jaff

回答

0

退房這樣的回答:

CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate]; 
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude]; 

CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate]; 
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude]; 

float distanceMeters = [pointALocation distanceFromLocation:pointBLocation]; 
float distanceMiles = (distanceMeters/1609.344); 

這裏pointAAnnotation & pointBAnnotation是兩個註釋,你需要他們之間的計算距離。

編號:Distance bw two Latitude & Longitude

+0

感謝您的答覆。我想通過道路計算兩個位置之間的距離。 distanceFromLocation:將在兩個位置之間繪製一條直線路徑,併爲此給出正確的距離? 可以說,如果我從地址作爲家庭給予,並作爲工作地址。我希望通過道路從家到工作之間的距離。通過公路我得到750米。但是如果我計算distanceFromLocation,我只能得到400米。 – Jaff

+0

是的..這將直接返回直線長度。更好地嘗試用谷歌API來獲得確切的距離 –

相關問題