2011-09-27 52 views
2

我正在使用locationManager:didUpdateToLocation:fromLocation:獲取位置更新。大多數情況都運行良好,但當我檢查newLocation.speed propery時,我幾乎總是得到大於0的速度值。即使當我第一次開始獲取位置更新並且從來沒有移動時,我也會得到正值幾個更新。iOS:locationManager:didUpdateToLocation:fromLocation:報告不移動時的速度

只是做了簡單的大於0的支票,然後設置一個UILablel如果它是:

從方法
if (newLocation.speed > 0) { 
    self.speed.text = [NSString stringWithFormat:@"%.2f",[self speed:newLocation.speed]]; 
} 

更多代碼:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

    // Check first if we are getting the accuracy we want 
    if (newLocation.horizontalAccuracy < 0) return; 

    // Make sure the update is new not cached 
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 
    if (locationAge > 5.0) return; 

    // Check to see if old and new are the same 
    if ((oldLocation.coordinate.latitude == newLocation.coordinate.latitude) && 
     (oldLocation.coordinate.longitude == newLocation.coordinate.longitude)) return; 

    // Make sure our new location is not super far away from old 
    CLLocationDistance dist = [newLocation distanceFromLocation:oldLocation]; 
    NSLog(@"Distance: %f", dist); 
    if (dist > 40) return; 

    // Make sure we have a new location 
    if (!newLocation) return; 

    // Check to see if we are stopped 
    if (newLocation.speed == 0) { 
     self.inMotion = NO; 
    } else if (newLocation.speed > 0) { 
     self.inMotion = YES; 
    } else { 
     // Probably an invalid negative value 
     self.inMotion = NO; 
    }//end 

    // Speed 
    // Should always be updated even when not recording! 
    if (newLocation.speed > 0 && inMotion) { 
     self.speed.text = [NSString stringWithFormat:@"%.2f",[self speed:newLocation.speed]]; 
     NSLog(@"Speed: %f", newLocation.speed); 
    } 

    // Tons more code below that I removed for this post 

}//end 

這是正常的嗎?

回答

2

比零大多少?和多久?你所比較的所有地點是否具有相同的準確性?

在GPS接收器中,速度通常取決於連續定位之間的位置變化(它也可以通過測量多普勒頻移來計算)。由於測量錯誤或位置更精確,位置修正會略有不同。位置的變化可能會使設備移動。

例如,假設您的第一個修補程序的水平精度爲1000m,第二個修正的水平精度爲300m,則第一個位置可能距離真實位置1000m,第二個位置可能距離第一個位置700m即使設備尚未移動,也可以修復。這意味着隨着時間的變化,這是「速度」。

+0

我已經使用locationManager的開頭處的代碼更新了我的帖子:didUpdateToLocation:fromLocation:method。現在我正在檢查更新是否超過40米,如果不這樣的話。這個值應該小得多嗎?當我得到修復時,他們相距約5-6米。改進我有什麼想法? –