iOS: locationManager:didUpdateToLocation:fromLocation: reports speed when not moving
I am using locationManager:didUpdateToLocation:fromLocation: to get location updates. Most everything is working well but when I check the newLocation.speed propery while standing still I almost always get a speed value greater than 0. Even when I start getting location updates for the first time and have NEVER even moved I will get positive values after a few updates.
Just doing a simple greater than 0 check then se开发者_StackOverflowtting a UILablel if it is:
if (newLocation.speed > 0) {
self.speed.text = [NSString stringWithFormat:@"%.2f",[self speed:newLocation.speed]];
}
More code from that method:
- (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
Is this normal?
How much greater than zero? And for how long? Do all the locations that you are comparing have the same accuracy?
In a GPS receiver the speed is usually determined by the change in location between successive location fixes (it can also be computed by measuring doppler shift). Location fixes will vary slightly due to measurement errors or as they become more accurate. The changes in location can make it appear that the device moved.
For example, suppose your first fix has an accuracy of 1000m horizontal and your second fix has an accuracy of 300m horizontal, the first position could have been 1000m off of the real location and the second could be 700m away from that first fix even though the device hasn't moved. This translates to a change in location over time which is "speed".
精彩评论