views:

666

answers:

3

Hi Everyone:

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!

A: 

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.

unforgiven
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?
PF1
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
unforgiven
You may also reset location warnings in the settings; this should also reset the location property without deleting/reinstalling your app.
unforgiven
A: 

See my answer here : http://stackoverflow.com/questions/2176124/how-to-remove-cache-of-cllocation/2176155#2176155 about "how to remove cache of cllocation". this should solve your issue regarding the cache of the CLLocationManager.

yonel
Hi yonel: What would you suggest I set SECS_OLD_MAX to?
PF1
A "recent" position has less 1 or 2 seconds so if you compare the timestamp of the CLLocation and the "now", and you're under 2 or 3 sec, you can consider this CLLocation instance as a good one.
yonel
+1  A: 

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