【问题标题】:Is it possible to defer location updates when monitoring significant location changes?监控重大位置变化时是否可以推迟位置更新?
【发布时间】:2016-04-15 18:19:47
【问题描述】:

我正在开发一个应用程序来监控重大的位置变化,以便在后台获取用户的位置。我已经成功实现了locationManager.startMonitoringSignificantLocationChanges 以及我的CLLocationManagerDelegate 的locationManager:didUpdateLocations 和locationManager:didFailWithError 方法。

但是,SLC 实际上比我需要的更准确。根据 Apple 的文档 - 并由我的测试证实 - slc 大约每 500m 和 5 到 10 分钟触发一次位置更新。因此,我在委托的didUpdateLocations 方法中实现了locationManager.allowDeferredLocationUpdatesUntilTravelled:timeout,如本指南中所述:http://apple.co/1W4gqEJ。

这是我的代码:

var deferringUpdates = false

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    for location in locations {
        NSLog("Significant location change recorded:\n%@", location)
    }

    if let location = locations.first {
        let secondsAgo: NSTimeInterval = location.timestamp.timeIntervalSinceNow

        // Only process the location if it is very recent (less than 15 seconds old).
        if abs(secondsAgo) < 15.0 {
            saveExtendedUserInfo(withLocation: location)
        }
    }

    if !deferringUpdates {
        manager.allowDeferredLocationUpdatesUntilTraveled(810, timeout: 600)
        deferringUpdates = true
        NSLog("Deferring location updates...")
    }
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    NSLog(error.localizedDescription)
}

func locationManager(manager: CLLocationManager,
    didFinishDeferredUpdatesWithError error: NSError?)
{
    deferringUpdates = false

    if let deferralError = error {
        NSLog(deferralError.localizedDescription)
    }
}

很遗憾,位置管理器从不推迟更新。在调用allowDeferredUpdatesUntilTravelled:timeout 之后,代理立即执行didFinishDeferredUpdatesWithError 并产生kCLErrorDomain 12,即CLError.DeferredNotUpdatingLocation。

为什么会出现这个错误?这似乎意味着延迟更新服务不会将监视重大位置更改视为“更新位置”。是否可以推迟 slc 事件的传递,或者以某种方式降低它们的频率?如果有,怎么做?

【问题讨论】:

  • 嘿。我正在实施同样的事情。你是怎么做到的?你能帮我解决这个问题吗?如何以及在哪里使用此方法 .allowDeferredUpdatesUntilTravelled: 根据用户的速度超时?

标签: ios core-location


【解决方案1】:

延迟更新的目的是在处理来自 GPS 的 1 Hz 位置更新时节省主 CPU 消耗的电池电量。通过延迟更新,CPU 保持睡眠以节省电池,而 GPS 芯片每秒累积一次 GPS 位置 (1 Hz)。

在发生重大位置变化 (SLC) 时,系统不使用 GPS。它基于蜂窝塔三角测量确定位置,在发生重大变化之前不会唤醒 CPU。

这两个功能是互斥的,您不能推迟重大位置变化的更新,因为 GPS 芯片不参与 SLC。

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 2012-03-22
    • 2013-07-04
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多