fuzzygoat
fuzzygoat

Reputation: 26223

Stopping CLLocationManager?

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

Answers (3)

Rayfleck
Rayfleck

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

Peter Lapisu
Peter Lapisu

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

  • If the result accuracy is better than acceptableAccuracy, we are done
  • If we get an update on occuracy, we wait maximumWaitTimeForBetterResult to get a better one, - If this doesn't happen, we are done and take the best one
  • If we are constantly getting updates, which exceed maximumAttempts, we take th best one (probably we are moving anyway)
  • If we don't get any other update in 30 sec, we are done (there won't be probably any other update)

Code

- (void)scopeToCurrentLocationWithAcceptableAccuracy:(CLLocationAccuracy)acceptableAccuracy
                  maximumWaitTimeForBetterResult:(NSTimeInterval)maximumWaitTimeForBetterResult
                                 maximumAttempts:(NSInteger)maximumAttempts
                                    onCompletion:(M6GPSLocationManagerCompletion)completion;

Upvotes: 1

Tejaswi Yerukalapudi
Tejaswi Yerukalapudi

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

Related Questions