individualtermite
individualtermite

Reputation: 3775

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!

Upvotes: 9

Views: 6264

Answers (3)

Tom Harrington
Tom Harrington

Reputation: 70986

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.

Upvotes: 17

mrt
mrt

Reputation: 1749

-(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 
    } 
 }

Upvotes: 8

Massimo Cafaro
Massimo Cafaro

Reputation: 25429

You can use this property of CLLocationManager:

@property(readonly, NS_NONATOMIC_IPHONEONLY) CLLocation *location;

The value of this property is nil if no location data has ever been retrieved, otherwise, this is where CoreLocation caches its data. Therefore, if you always want to start from scratch, simply check if this property is nil or not. If it's nil, then you are ok; otherwise, you need to stop your location manager and start it again: this will force an update, which will result in the cached value being overwritten by the fresh one you have just triggered.

Upvotes: 0

Related Questions