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()
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With