If you tell the Location Manager to start updating, it will keep returning updates indefinitely until it’s told to stop. It returns the new location to its delegate method: didUpdateToLocation (Like a callback).
It will keep trying to get you the most accurate location it can – when you create the Location Manager you can specify the level of accuracy (in metres) using the property: desiredAccuracy.
I haven’t ever experienced it returning more than 3 location updates, however I’ve read on some forums that people havegot up to 10 location updates back. To prevent the user waiting indefinitely for a response, I started a timer after the call to startUpdatingLocation and then if no response had come back after a certain time I could return and let the user know.
Every time you stop updating (i.e. every time you call stopUpdatingLocation) the Location Manager will cache the last location returned, and then give you this cached location next time you start updating again, so if a fresh position is important, check the timestamp property of the returned location.
It’s not possible to make the delegate method return (it wants to get you the best location it can!) but you can examine the properties of the location returned each time and work out whether or not you want to use it, or wait for a better one.
Here’s an example of how we used it: (Sorry it’s a bit compressed, it’s a skinny column!)
- (void)didUpdateToLocation {
// get lat and lon here from the response
// Check if we're interested in the response
if(ignoreAllOtherResponses == NO){
// and are we within agreed number of updates
if(numberOfUpdates < maxNumLocationUpdates){
// check accuracy and timestamp of response
if(response < requiredLocationAccuracy
&& interval < maxCachedLocationAge){
// we've got the location we need so stop
// updating and ignore any subsequent responses
[locationManager stopUpdatingLocation];
ignoreAllOtherResponses = YES;
}
else {
// Feedback: "Got location to within n meters
still trying etc etc.."
}
// And if we're still listening, then by the time
// we've reached our agreed maximum number of
// updates just take this as the final answer as long
// as it's *at least as accurate as the previous one*
}
else {
double finalResponse = newLoc.horizontalAccuracy;
double lastResponse = oldLoc.horizontalAccuracy;
if(finalResponse <= lastResponse) {
[locationManager stopUpdatingLocation];
ignoreAllOtherResponses = YES;
}
else {
// Feedback: "Cant get an accurate enough
location at the moment"
}
// stop the timer and stop updating location
[locationManager stopUpdatingLocation];
[updateTimer invalidate];
} // end if (numberOfUpdates < maxNumLocationUpdates)
numberOfUpdates++;
[newLat release];
[newLon release];
} // end if(ignoreAllOtherResponses == NO)
}