8

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!

individualtermite
  • 3,615
  • 16
  • 49
  • 78

3 Answers3

17

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.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
8
-(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 
    } 
 }
mrt
  • 1,669
  • 3
  • 22
  • 32
0

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.

Massimo Cafaro
  • 25,429
  • 15
  • 79
  • 93
  • Hi unforgiven: I have interpreted the documentation to mean that the location is cached on a system level (though if that's not accurate, please let me know). Does this work in that scenario? – individualtermite Feb 14 '10 at 19:11
  • To the best of my knowledge, what you call system level is the location property; therefore it should work. You may check this by using core location in your app so that the location property is not nil. Then, removing the application from the device, switching it off and on, and installing again the application causes the confirmation panel to appear (permission to use your location) and I have verified that the location property is always nil in this case. Now,instead of doing all of this you simply need to get rid of the old cached value stopping your location manager and starting it again – Massimo Cafaro Feb 14 '10 at 19:41
  • You may also reset location warnings in the settings; this should also reset the location property without deleting/reinstalling your app. – Massimo Cafaro Feb 14 '10 at 19:42