【问题标题】:Swift CLLocationManager(didUpdateLocations) is called too oftenSwift CLLocationManager(didUpdateLocations) 被调用太频繁
【发布时间】:2016-05-29 21:00:55
【问题描述】:

谁能解释一下,为什么委托方法“CLLocationManager(didUpdateLocations)”被调用了 4 次,尽管我立即停止更新?我只需要一次当前位置:)

import UIKit
import MapKit

class ContactView: UIViewController, CLLocationManagerDelegate {

    let clLocationManager = CLLocationManager()

    var latitude: AnyObject = 0.000000
    var longitude: AnyObject = 0.000000

    override func viewDidLoad() {
        super.viewDidLoad()
        self.nameTextField.delegate = self
        clLocationManager.delegate = self
        clLocationManager.desiredAccuracy = kCLLocationAccuracyBest
        clLocationManager.requestWhenInUseAuthorization()
        clLocationManager.startUpdatingLocation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {
            (placemarks, error) -> Void in
            self.clLocationManager.stopUpdatingLocation()
            if placemarks!.count > 0 {
                self.latitude = (manager.location?.coordinate.latitude)!
                self.longitude = (manager.location?.coordinate.longitude)!
                print(self.latitude)
                print(self.longitude)
            } else {
                print("Error")
            }
        })
    }
}

谢谢!

【问题讨论】:

    标签: swift delegates mapkit core-location cllocationmanager


    【解决方案1】:

    你需要像下面这样改变你的代码:

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        manager.stopUpdatingLocation()
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {
            (placemarks, error) -> Void in
            if placemarks!.count > 0 {
                self.latitude = (manager.location?.coordinate.latitude)!
                self.longitude = (manager.location?.coordinate.longitude)!
                print(self.latitude)
                print(self.longitude)
            } else {
                print("Error")
            }
        })
    }
    

    您看到问题是因为您在继续更新您的位置,而对您的位置进行反向地理编码的调用仍在进行中。

    【讨论】:

      【解决方案2】:

      尝试将 stopUpdatingLocation 移出 reverseGeocodeLocation

       func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          manager.stopUpdatingLocation()
          CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {
                      (placemarks, error) -> Void in
                      if placemarks!.count > 0 {
                          self.latitude = (manager.location?.coordinate.latitude)!
                          self.longitude = (manager.location?.coordinate.longitude)!
                          print(self.latitude)
                          print(self.longitude)
                      } else {
                          print("Error")
                      }
                  })
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 2016-07-11
        • 1970-01-01
        • 2012-10-30
        • 1970-01-01
        相关资源
        最近更新 更多