大佬教程收集整理的这篇文章主要介绍了swift – 为什么LocationManager多次调用startUpdatingLocation?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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@
如果要减少此行为,可以使用较大的distanceFilter,较低的desiredAccuracy组合,或者在获得要进行地理编码的位置后调用stopupdatingLOCATIOn.
现在你正在调用stopupdatingLOCATIOn,但是你是从异步调用的reverseGeocodeLOCATIOn闭包中做到的.这意味着在调用reverseGeocodeLOCATIOn的完成处理程序之前,更多位置更新可以滑入.如果同步调用stopupdatingLOCATIOn(例如在reverseGeocodeLOCATIOn之前),则会避免此行为.
以上是大佬教程为你收集整理的swift – 为什么LocationManager多次调用startUpdatingLocation?全部内容,希望文章能够帮你解决swift – 为什么LocationManager多次调用startUpdatingLocation?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。