Why does the LocationManager call startUpdatingLocation several times?

Why startUpdatingLocation location manager call startUpdatingLocation more than once? Sometimes he calls once, sometimes he calls him three times. I do not know why; maybe you could help me. I have this code from GitHub.

 import UIKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate { let locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() self.locationManager.delegate = self self.locationManager.desiredAccuracy = kCLLocationAccuracyBest self.locationManager.requestWhenInUseAuthorization() self.locationManager.startUpdatingLocation() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error) -> Void in if (error != nil) { print("Error: " + error!.localizedDescription) return } if placemarks!.count > 0 { if let pm = placemarks?.first { self.displayLocationInfo(pm) } } else { print("Error with the data.") } }) } func displayLocationInfo(placemark: CLPlacemark) { self.locationManager.stopUpdatingLocation() print(placemark.locality) print(placemark.postalCode) print(placemark.administrativeArea) print(placemark.country) } func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { print("Error: " + error.localizedDescription) } } 
+4
source share
1 answer

Yes, this is standard behavior. When you start location services, you usually get a series of increasingly accurate CLLocation updates (ie, with a decrease in horizontalAccuracy over time), as the device β€œwarms up”. For example, it may start reporting location information that it might already have based on cell towers, but since the GPS chip gets more information with which it can better triangulate your location, it will give you updates. Etc.

If you want to reduce this behavior, you can use a combination of a larger distanceFilter , lower desiredAccuracy or calling stopUpdatingLocation after getting the location that you will geocode.

Right now you are calling stopUpdatingLocation , but you are doing this from an asynchronously called reverseGeocodeLocation closure. This means that more location updates may slip before the reverseGeocodeLocation termination handler is reverseGeocodeLocation . If you call stopUpdatingLocation synchronously (for example, before reverseGeocodeLocation ), you will avoid this behavior.

+2
source

All Articles