Reputation: 26223
I would like to ask for advice on stopping a CLLocationManager -startUpdatingLocation. Currently I am considering two methods, but I am unsure which to use and would like to get an idea how other folks do this:
Method_001:
[locationManager startUpdatingLocation];
[self performSelector:@selector(stopUpdatingLocation:) withObject:@"TimedOut" afterDelay:30];
Method_002:
[locationManager startUpdatingLocation];
Then inside: -locationManager:didUpdateToLocation:fromLocation:
add:
static int timeOut = 0;
timeOut++;
// Other code that checks for and stops
// when a suitable result, accuracy, age etc is found.
if(timeOut >= 4) {
[[self locationManager] stopUpdatingLocation];
timeOut = 0;
return;
}
Just curious?
Upvotes: 3
Views: 5028
Reputation: 12106
Not sure exactly what you're trying to do, but I think the CLLocationManager handles these cases internally. Just configure it thus:
locManager.desiredAccuracy = 2000.0f; // 2 kilometers - hope for accuracy within 2 km.
locManager.distanceFilter = 1000.0f; // one kilometer - move this far to get another update
and then in the callback didUpdateToLocation:fromLocation: if you have a positive signbit,
[locManager stopUpdatingLocation]; // stop GPS
EDIT: add signbit
if (signbit(newLocation.horizontalAccuracy)) {
// Negative accuracy means an invalid or unavailable measurement, so punt.
} else {
// this is a usable measurement.
}
Upvotes: 3
Reputation: 20965
Why not to combine both approaches and give a third (i the best result didn't improve in some amount of time)
I wrote a GIT repo on this, which you are free to use https://github.com/xelvenone/M6GPSLocationManager
Code
- (void)scopeToCurrentLocationWithAcceptableAccuracy:(CLLocationAccuracy)acceptableAccuracy
maximumWaitTimeForBetterResult:(NSTimeInterval)maximumWaitTimeForBetterResult
maximumAttempts:(NSInteger)maximumAttempts
onCompletion:(M6GPSLocationManagerCompletion)completion;
Upvotes: 1
Reputation: 9157
Hmm, I think I prefer the first one. I don't know if we can be sure about how often the didUdpateToLocation:
method gets called. I think the time out is more reliable.
Upvotes: 2