在分析 iOS 10 中的位置服务后,发现缓存行为存在一些不一致。
定期(在我的例子中每 20 秒)获取位置会返回位置,但它们的时间戳不按时间顺序排列。这表明缓存位置可能有问题。因此,如果您通过位置时间戳检查准确性,最好也保存以前的时间戳。这样您就可以决定是否可以使用获取的位置。
下图取自我的控制台日志。这里我使用了格式“Lat Long”:纬度_经度 | 位置_时间戳|现在 :当前时间戳"
是的,有时ios会从缓存中获取最准确的位置,因此您需要避免该位置,这里是准确定位的代码。
Update :“由于返回初始位置可能需要几秒钟的时间,位置管理器通常会立即提供先前缓存的位置数据,然后在可用时提供更多最新的位置数据。因此,检查位置管理器始终是一个好主意。在采取任何操作之前任何位置对象的时间戳。”
参考 :
https://developer.apple.com/reference/corelocation/cllocationmanager https://developer.apple.com/reference/corelocation/cllocationmanager
Note:您可以改变 ipod 和 ipad 等设备的精度
//MARK: Location delgates
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
if locations.count > 0
{
let location = locations[locations.count-1]
let maxAge:TimeInterval = 60;
let requiredAccuracy:CLLocationAccuracy = 100;
let locationIsValid:Bool = Date().timeIntervalSince(location.timestamp) < maxAge && location.horizontalAccuracy <= requiredAccuracy;
if locationIsValid
{
NSLog(",,, location : %@",location);
NSLog("valid locations.....");
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)