【发布时间】: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