Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Returning Cached CLLocationManager Location

I am wondering if there is some way to make it so CLLocationManager doesn't automatically returned a cached location. I understand that the documents say "The location service returns an initial location as quickly as possible, returning cached information when available" but this cached location could be extremely far away from the user's current location and I would like it more precise if possible.

Thanks for any help!

like image 498
PF1 Avatar asked Feb 14 '10 18:02

PF1


2 Answers

You can't stop it from caching, but it's not hard to filter out cached data. Core Location includes a timestamp with its locations. Compare the timestamp of the location with a timestamp saved when your app started, and you'll be able to tell which locations are old (cached, found before your app stated) and which are new. Throw away the old ones.

The location timestamp is an NSDate, so just get the value of [NSDate date] when your app starts up and use that as your reference point when filtering locations. You could even throw away the reference value once you start getting new data and treat a nil reference date as implying that new locations should be trusted.

like image 75
Tom Harrington Avatar answered Nov 17 '22 12:11

Tom Harrington


-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation 
{
    if ([newLocation.timestamp timeIntervalSinceNow] > -10.0) // The value is not older than 10 sec. 
    { 
         // do something 
    } 
 }
like image 8
mrt Avatar answered Nov 17 '22 14:11

mrt