停止返回缓存的 CLLocationManager 位置
我想知道是否有某种方法可以使 CLLocationManager 不会自动返回缓存位置。据我所知,文档说“位置服务尽快返回初始位置,在可用时返回缓存信息”,但该缓存位置可能距离用户当前位置非常远,如果可能的话,我希望它更精确。
感谢您的帮助!
I am wondering if there is some way to make it so CLLocationManager doesn't automatically returned a cached location. I understand that the documents say "The location service returns an initial location as quickly as possible, returning cached information when available" but this cached location could be extremely far away from the user's current location and I would like it more precise if possible.
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你无法阻止它缓存,但过滤掉缓存的数据并不难。核心位置包括带有其位置的时间戳。将位置的时间戳与应用程序启动时保存的时间戳进行比较,您将能够辨别哪些位置是旧的(缓存的,在应用程序声明之前找到的)和哪些是新的。扔掉旧的。
位置时间戳是 NSDate,因此只需在应用启动时获取
[NSDate date]
的值,并将其用作过滤位置时的参考点即可。一旦开始获取新数据,您甚至可以丢弃参考值,并将零参考日期视为暗示应信任新位置。You can't stop it from caching, but it's not hard to filter out cached data. Core Location includes a timestamp with its locations. Compare the timestamp of the location with a timestamp saved when your app started, and you'll be able to tell which locations are old (cached, found before your app stated) and which are new. Throw away the old ones.
The location timestamp is an NSDate, so just get the value of
[NSDate date]
when your app starts up and use that as your reference point when filtering locations. You could even throw away the reference value once you start getting new data and treat a nil reference date as implying that new locations should be trusted.您可以使用 CLLocationManager 的此属性:
如果从未检索过位置数据,则此属性的值为
nil
,否则,这是 CoreLocation 缓存其数据的位置。因此,如果您总是想从头开始,只需检查此属性是否为 nil 即可。如果它是nil
,那么就可以了;否则,您需要停止位置管理器并再次启动它:这将强制更新,这将导致缓存的值被刚刚触发的新值覆盖。You can use this property of CLLocationManager:
The value of this property is
nil
if no location data has ever been retrieved, otherwise, this is where CoreLocation caches its data. Therefore, if you always want to start from scratch, simply check if this property isnil
or not. If it'snil
, then you are ok; otherwise, you need to stop your location manager and start it again: this will force an update, which will result in the cached value being overwritten by the fresh one you have just triggered.