【问题标题】:startMonitoringSignificantLocationChanges() doesn't work on physical devicestartMonitoringSignificantLocationChanges() 在物理设备上不起作用
【发布时间】:2018-10-16 09:18:39
【问题描述】:

当 locationManager 设置为 startMonitoringSignificantLocationChanges() 时,我无法在物理设备上显示位置。

一切都在模拟器中运行,但是当我让它在我的 iPhone 上运行时,没有返回坐标,如果我将它更改为 startUpdatingLocation() 它可以在我的物理设备上运行。

知道可能出了什么问题吗?

我的代码:

  let locationManager = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()

        startReceivingSingificantLocationChanges()
    }

    func startReceivingSingificantLocationChanges() {

        if !CLLocationManager.significantLocationChangeMonitoringAvailable() {
            //the service is not available
            print("Service is not available")
            return
        }

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startMonitoringSignificantLocationChanges()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first{
            print(location.coordinate)
            coordinatesLbl.text = "LAT:\(location.coordinate.latitude) LONG:\(location.coordinate.longitude)"
        }
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == CLAuthorizationStatus.denied{
            showLocationDisabledPopUp()
            print("didChangeAuthorization")
        }
    }

【问题讨论】:

    标签: swift cllocationmanager


    【解决方案1】:

    The documentation on startMonitoringSignificantLocationChanges() 状态:

    只要设备从之前的通知移动 500 米或更远,应用程序就会收到通知。它不应期望通知的频率超过每五分钟一次。如果设备能够从网络中检索数据,则位置管理器更有可能及时发送通知。

    因此,在使用此方法时,您不应经常期望它们。

    startUpdatingLocation 更频繁地更新,这就是它起作用的原因。

    【讨论】:

    • 感谢您的回复,但不是应该给您当前位置,然后在 500-1 公里后更新吗?现在它根本不返回任何坐标。
    • 文档说The first event to be delivered is usually the most recently cached location event (if any) 所以你可能得不到第一个
    【解决方案2】:

    我已经尝试过这个并且遇到了同样的问题。我最终将位置desiredAccuracy 设置为kCLLocationAccuracyKilometer。这具有给我显着位置变化的预期效果。另一种方法是使用startUpdatingLocation,一旦你有了当前位置,将位置管理器更改为startMonitoringSignificantLocationChanges

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 2021-05-11
      • 1970-01-01
      • 2016-12-07
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多