2012-03-13 87 views
3

我想計算用戶使用GPS行走時覆蓋的距離。例如,用戶點擊開始按鈕並開始走路或跑步,而不是當他完成他的停止時。用戶爲了獲得不同的經緯度而需要旅行的最小距離是多少?iPhone:使用GPS行走時計算距離

我們怎樣才能在iPhone中做到這一點,假設我們每隔0.3秒就會持續很長一段時間,比起上一次我們有一個點列表呢?

+0

計算GPS位置之間的距離很容易。其他一切都很難。特別是如果你想以低速跟蹤距離。如果你經常測量,你會積累更多的錯誤。如果移動速度較慢,則每個距離累積更多誤差。獲取[surveyor's wheel](http://en.wikipedia.org/wiki/Surveyor's_wheel)並在真實世界中測試您的算法。你會想知道GPS是多麼的錯誤。 – 2012-03-13 13:56:55

+0

問題中最重要的部分(至少對於ma來說)仍然沒有答案:「用戶爲了獲得不同的經緯度而需要走的最短距離是多少?」換句話說,如果我向前走一兩步,GPS就會確認這個位移並計算行駛的距離(即使它小於,比如1-1,5米)? – 2012-06-20 19:46:28

回答

3

你可以通過計算2點(緯度,經度)之間的距離,這樣做:

(我沒有測試過):

-(double)distanceBetweenCoordinate:(CLLocationCoordinate2D)c1 andCoordinate:(CLLocationCoordinate2D)c2 { 
    double long1 = degreesToRadians(c1.longitude); 
    double lat1 = degreesToRadians(90 - c1.latitude); 

    double long2 = degreesToRadians(c2.longitude); 
    double lat2 = degreesToRadians(90 - c2.latitude); 

    double gamma = fabs(long1 - long2); 
    if (gamma > M_PI) { 
     gamma = 2 * M_PI - gamma; 
    }  

    double result = cos(lat2) * cos(lat1) + sin(lat2) * sin(lat1) * cos(gamma); 
    return acos(result) * 6366.1977; // Kilometers 
}; 

CGFloat degreesToRadians(CGFloat degrees) { 
    return degrees * M_PI/180; 
}; 

更新:使用distanceFromLocation - Calculate distance between two points代替

+6

或者只是使用CLLocation方法 - (CLLocationDistance)distanceFromLocation:(const CLLocation *)位置 – sosborn 2012-03-13 13:40:12

+0

更好的方法:p對CoreLocation或MapKit框架沒有多少經驗。所以我'從我的舊php功能池'釣魚'。 – basvk 2012-03-13 13:41:52

+0

請記住,這是「直線」距離(如鳥的飛行),而不是總行程。 – lnafziger 2012-03-14 00:13:44