2014-01-20 41 views
0

我會感謝任何人,誰可以給我解釋一下這些方法如何一個(或兩個)的算法工作:後面寫了什麼代碼 - [CLLocation distanceFromLocation:]或MKMetersBetweenMapPoints?

我有兩個CLLocationCoordinate2Ds或兩個MKMapPoints - 我應該執行來計算距離beetween什麼算盤他們對現實世界的地球表面? (顯然,歐幾里德距離的獨立計算不適用於此任務。)

背景:我想知道如果這些方法的內部知識能夠幫助我優化一些涉及大量點的計算一個MapKit地圖。

+1

喜歡的東西[這](http://www.movable-type.co.uk/scripts/latlong .html)或者可能[this](http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points)或者可能[這些答案之一問題](http://gis.stackexchange.com/questions/tagged/distance)? –

+0

描述您想要對您的一組點進行的計算並提出有關優化的問題似乎更有幫助。目前,您正在詢問一個框架方法的實現細節,該方法可能會發生變化,並且可能不被Apple以外的人所知。 –

+0

@DavidRönnqvist,[這個更接近](http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe#Objective_C)。 –

回答

1

關於Geographical distance的維基百科文章有一些計算測地距離的公式。

這是我目前使用的是給了我可以接受的結果的一段代碼:

const float EarthRadius = 6378137.0f; 

float SquaredGeodesicDistance(CLLocationCoordinate2D a, CLLocationCoordinate2D b) 
{ 
    float dtheta = (a.latitude - b.latitude) * (M_PI/180.0); 
    float dlambda = (a.longitude - b.longitude) * (M_PI/180.0); 
    float mean_t = (a.latitude + b.latitude) * (M_PI/180.0)/2.0; 
    float cos_meant = cosf(mean_t); 

    return (EarthRadius * EarthRadius) * (dtheta * dtheta + cos_meant * cos_meant * dlambda * dlambda); 
} 

float GeodesicDistance(CLLocationCoordinate2D a, CLLocationCoordinate2D b) 
{ 
    return sqrtf(SquaredGeodesicDistance(a, b)); 
} 
+0

明天我會測試你的例子。現在我可以說,在處理基於CLLocationDegrees(基於double)的CLLocationCoordinate2D時使用浮點數是非常危險的 - 這很可能會導致計算失去精度。 –

+0

然後用double來代替我猜?不應該在算法上有很大區別嗎?無論如何,該算法已經是一個粗略的近似值,但座標和距離通常不需要精確到米(至少在我的應用程序) – Taum

+0

還要注意,這會給你遠距離的結果,遠距離(認爲>千公里)。就像我說的,如果你的應用程序需要更精確的看看我鏈接到的維基百科頁面,以獲得更復雜但更準確的算法。 – Taum

相關問題