Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – 为什么LocationManager多次调用startUpdatingLocation?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

为什么位置管理器不止一次调用startupdatingLOCATIOn?有时它会调用一次,有时它会调用三次.我不知道为什么;也许你可以帮助我.我有来自 GitHub的代码. import UIKit import CoreLOCATIOn class ViewController: UIViewController, CLLOCATIOnManagerDelegate { let lo
为什么位置管理器不止一次调用startupdatingLOCATIOn?有时它会调用一次,有时它会调用三次.我不知道为什么;也许你可以帮助我.我有来自 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)
    }

}@H_616_16@
是的,这是标准行为.当您启动位置服务时,您通常会收到一系列越来越准确的CLLOCATIOn更新(即,随着时间的推移,horizo​​ntalAccuracy会逐渐减少),因为设备会“预热”.例如,它可能会开始报告它可能已经基于单元塔的位置信息,但随着GPS芯片获得更多信息,它可以更好地对您的位置进行三角测量,它将为您提供更新.等等.

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

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

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。