You already ensure that your location updates are less than five seconds. Here is what this line of code does:
if (locationAge > 5.0) return;
As progrmr pointed out, the key problem here almost certainly is that you rely on initial location estimates that have low accuracy, and therefore the location seems to move quickly, since it better corrects your location. You need to first make sure that oldLocation will only be set when you have the exact fix for the original location, and then compare newLocations with this oldLocation if they also have acceptable resolution.
For example, you might have something like this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ // Ignore location updates that are less than 10m accuracy, or where the horizontalAccuracy < 0 // which indicates an invalid measurement. NSLog(@"New location accuracy %.0fm", newLocation.horizontalAccuracy); if ((newLocation.horizontalAccuracy < 0) || (newLocation.horizontalAccuracy > 10)) return; // Ignore location updates that are more than five seconds old, to avoid responding to // cached measurements. NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; if (locationAge > 5) return; if (self.oldLocat == NULL) { // For the first acceptable measurement, simply set oldLocat and exit. self.oldLocat = newLocation; return; } // A second location has been identified. Calculate distance travelled. // Do not set self.oldLocat from the oldLocation provided in this update, as we wish to // use the last place we calculated distance from, not merely the last place that was // provided in a location update. CLLocationDistance distance = [newLocation distanceFromLocation:self.oldLocat]; NSLog(@"Distance: %.0fm", distance); // This new location is now our old location. self.oldLocat = newLocation; }
Note that with the code above, you do not need the computeDistanceFrom: tO: method, and the self.newLocat property is not actually required, at least in this section of the code.
source share