2009-07-24 38 views
1

我正在玩iPhone SDK,我想在我的應用程序中顯示當前的速度。有這麼多的應用程序可以做到非常精確,特別是對於跑步或騎自行車等低速運動。我見過的最好的是RunKeeper。爲什麼我的CLLocation速度不準確?

但是,在我的應用程序中,速度是絕對不準確的。在低速時它總是無效的,只有在更高的速度下它纔會顯示一些數值,但它們很少被更新並且不是很有用。

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation { 
    if (newLocation.timestamp > oldLocation.timestamp && 
     newLocation.verticalAccuracy > 0.0f &&         // GPS is active 
     newLocation.horizontalAccuracy < 500.0f &&         // GPS is active 
     //newLocation.horizontalAccuracy < kCLLocationAccuracyHundredMeters && // good quality GPS signal 
     //newLocation.speed > 1.0f &&           // enough movment for accurate speed and course measurement 
     oldLocation != nil)              // oldLocation is nil on the first reading 
    { 
     double speed = (newLocation.speed * 3.6); 
     [self updateDisplayWithSpeed:speed]; 
     //double direction = newLocation.course; 
    } 
} 

有沒有人有工作代碼?或者你能告訴我我的問題嗎?

回答

7

我會嘗試以下操作。

請記住,您應該根據您的場景需要設置distanceFilter和desiredAccuracy:行走與駕車等不同。此外,當您從GPS請求高精度時,必須始終丟棄提供的第一個位置通過GPS並使用第二個作爲起始位置。引用Apple文檔:

您應該爲此屬性指定一個適合您的使用場景的值。換句話說,如果您只需要幾公里內的當前位置,則不應指定kCLLocationAccuracyBest來確保準確性。以更高的準確度確定位置需要更多的時間和更多的權力。 請求高準確度的位置數據時,位置服務提供的初始事件可能沒有您請求的準確性。定位服務儘可能快地提供初始事件。然後,它會繼續根據您請求的準確性確定位置,並在有數據可用時根據需要提供其他事件。

這是我的建議。

首先,使用GPS更新位置,間隔至少1秒。低於此間隔時,速度值無用,因爲它以米/秒爲單位進行測量,並且由於之前的討論,您不可能在一秒鐘內得到有效更新並且精確度較高。

其次,只對位置座標使用有意義的值:您應該丟棄具有負水平準確性的值。這就是我說的好地點時所指的。第三,你可以自己計算距離:使用getDistanceFrom()計算上一個好位置和上一個好位置之間的距離,然後除以位置更新之間經過的秒數。這會給你以米/秒爲單位的距離。我會盡力做到這一點,並將結果與​​Apple提供的速度進行比較。

+0

你有一個代碼示例來處理第一個問題?或者,這只是檢查與前一個位置的時間戳? – 2014-03-20 08:40:25

3

請嘗試在你的代碼如下:

speedLabel.text = [NSString stringWithFormat:@"SPEED(Km/Hr): %f", [location speed]*3.6]; 
latitudeLabel.text = [NSString stringWithFormat:@"LATITUDE: %f", location.coordinate.latitude]; 
longitudeLabel.text = [NSString stringWithFormat:@"LONGITUDE: %f", location.coordinate.longitude]; 
CLLocationDistance meters = [location distanceFromLocation:orginalLoc]; 
distanceLabel.text = [NSString stringWithFormat:@"DISTANCE(M): %f", meters];