swift – 为什么LocationManager多次调用startUpdatingLocation?

swift – 为什么LocationManager多次调用startUpdatingLocation?,第1张

概述为什么位置管理器不止一次调用startUpdatingLocation?有时它会调用一次,有时它会调用三次.我不知道为什么;也许你可以帮助我.我有来自 GitHub的代码. import UIKitimport CoreLocationclass ViewController: UIViewController, CLLocationManagerDelegate{ let lo 为什么位置管理器不止一次调用startUpdatingLocation?有时它会调用一次,有时它会调用三次.我不知道为什么;也许你可以帮助我.我有来自 GitHub的代码.
import UIKitimport CoreLocationclass 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)    }}
是的,这是标准行为.当您启动位置服务时,您通常会收到一系列越来越准确的CLLocation更新(即,随着时间的推移,horizo​​ntalAccuracy会逐渐减少),因为设备会“预热”.例如,它可能会开始报告它可能已经基于单元塔的位置信息,但随着GPS芯片获得更多信息,它可以更好地对您的位置进行三角测量,它将为您提供更新.等等.

如果要减少此行为,可以使用较大的distanceFilter,较低的desiredAccuracy组合,或者在获得要进行地理编码的位置后调用stopUpdatingLocation.

现在你正在调用stopUpdatingLocation,但是你是从异步调用的reverseGeocodeLocation闭包中做到的.这意味着在调用reverseGeocodeLocation的完成处理程序之前,更多位置更新可以滑入.如果同步调用stopUpdatingLocation(例如在reverseGeocodeLocation之前),则会避免此行为.

总结

以上是内存溢出为你收集整理的swift – 为什么LocationManager多次调用startUpdatingLocation?全部内容,希望文章能够帮你解决swift – 为什么LocationManager多次调用startUpdatingLocation?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1089251.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存