我正在开发一个应用程序来监控重要的位置更改,以便在后台获取用户的位置.我已经成功地实施locationManager.startMonitoringSignificantLocationChanges和locationManager:didUpdateLocations与locationManager:didFailWithError我的方法CLLocationManagerDelegate.
但是,SLC实际上比我需要的更准确.根据Apple的文档 - 并且通过我的测试证实 - slc大约每500米触发一次位置更新,并在5到10分钟之间触发.因此,我locationManager.allowDeferredLocationUpdatesUntilTravelled:timeout在我的委托didUpdateLocations方法中实现,如本指南中所述: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)
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,位置经理从不推迟更新.allowDeferredUpdatesUntilTravelled:timeout调用之后,代理立即执行didFinishDeferredUpdatesWithError并生成kCLErrorDomain 12,即CLError.DeferredNotUpdatingLocation.
为什么我会收到这个错误?这似乎意味着延迟更新服务不会将监控重要位置更改识别为"更新位置".是否有可能推迟slc事件的传递,或以某种方式降低其频率?如果是这样,怎么样?
延迟更新的目的是在处理来自GPS的1 Hz位置更新时节省主CPU消耗的电量.通过延迟更新,CPU保持睡眠状态,同时GPS芯片每秒累积一次GPS位置(1 Hz).
通过显着位置更改(SLC),系统不使用GPS.它基于单元塔三角测量确定位置,在发生重大变化之前不会唤醒CPU.
这两个功能是互斥的,您不能推迟重要位置更改的更新,因为GPS芯片不参与SLC.
| 归档时间: |
|
| 查看次数: |
701 次 |
| 最近记录: |