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