3

How do you check a CLLocation object and decide whether you want to use it or discard the result and get a new location update instead?

I saw the timestamp property on CLLocation, but I'm not sure how to compare that to the current time.

Also, after I do compare the time and find the difference in seconds, what value should the difference be under for me to use that CLLocation object? What's a good threshold.

Thanks!

1
  • 2
    if (location.timestamp + someSeconds < [[NSDate date] timeIntervalSince1970]) { /* need new data */ } Commented Jan 2, 2013 at 9:06

1 Answer 1

11

Is really important to check the timestamp, because iOS usually caches the location and as a first measure returns the last cached, so here is how I do inside the delegate method of Core Location Manager:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation * newLocation = [locations lastObject];
        //check if coordinates are consistent
        if (newLocation.horizontalAccuracy < 0) {
            return;
        }
        NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];
        //check against absolute value of the interval
        if (abs(interval)<30) {
            //DO STUFF
        }
    }

I use 30 seconds. As you can see I also check the consistency of the result asking the horizontal accuracy.
Hope this helps,
Andrea

Sign up to request clarification or add additional context in comments.

3 Comments

FYI, this checks if the date is not too far in future. Using a timestamp in past will mean that the interval is negative and thus <30 will always be true.
Also, the horizontalAccuracy is less than zero only if location is invalid. So maybe an additional condition would be useful, for instance if we want the accuracy to be within 50 meters, add || horizontalAccuracy > 50 in the contition above
That snippet is for general use. The check against 0 is only to check if it is invalid, not for any other purpose. Since the the time interval is requested as an absolute value and considering that no location has a value in the future the condition can be false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.