Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestLocation() in iOS 9 calls the didUpdateLocation delegate multiple times

So there is the new requestLocation() method on iOS9. I am trying to use it to get only one location update but it hits the didUpdateLocations multiple times.

Isn't this suppose to call it only once ? i have set distanceFilter to 1000.0, so it is pretty broad to help return quickly. Any ideas ?

even if i call the stopUpdatingLocation() inside the delegate method, i still get three hits of the delegate.

Note: same behavior occurs when i use StartUpdatingLocation instead, i want only a single return as i want to obtain the user's current country, so feed the location to reversegeocoder

thanks in advance

Here is the code:

func getLocationOneTime(accuracy: Double){
    print("Acquiring location one time - accuracy requested:\(accuracy)")
    locationManager.distanceFilter = accuracy
    locationManager.desiredAccuracy = accuracy
    // Start gpsOneTimeTimeout timer
    gpsTimeoutTimer = NSTimer.scheduledTimerWithTimeInterval(gpsOneTimeTimeout, target: self, selector: #selector(self.reportTimeout), userInfo: nil, repeats: false)
    locationManager.requestLocation()
     }
like image 955
jelipito Avatar asked Apr 11 '16 07:04

jelipito


1 Answers

Usually for this kind of problem I just use an if statement to prevent the delegate function changing anything after the first call

in this case if you are just looking for the location I would do:

let locationManager = CLLocationManager()
var userLocation: CLLocation!

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if userLocation == nil {
        if let location = locations.first {
            userLocation = location
            self.locationManager.stopUpdatingLocation()
        }
    } 
}

Edit: I had a look at the apple documentation for this and you are doing things right, I doubt you'll be able to prevent it through any reasonable means sadly.

like image 139
Olivier Wilkinson Avatar answered Nov 07 '22 16:11

Olivier Wilkinson