Rohan Agarwal
Rohan Agarwal

Reputation: 2187

iOS CoreLocation checking CLLocation timestamp to use it

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!

Upvotes: 3

Views: 7331

Answers (1)

Andrea
Andrea

Reputation: 26385

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

Upvotes: 11

Related Questions