简体   繁体   English

停止返回缓存的CLLocationManager位置

[英]Stop Returning Cached CLLocationManager Location

I am wondering if there is some way to make it so CLLocationManager doesn't automatically returned a cached location. 我想知道是否有某种方法可以使CLLocationManager不会自动返回缓存的位置。 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! 谢谢你的帮助!

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. 位置时间戳是NSDate,因此只需在应用启动时获取[NSDate date]的值,并在过滤位置时将其用作参考点。 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. 一旦开始获取新数据,甚至可以将参考值视为零,这意味着您应该信任新位置,甚至可以丢弃参考值。

-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation 
{
    if ([newLocation.timestamp timeIntervalSinceNow] > -10.0) // The value is not older than 10 sec. 
    { 
         // do something 
    } 
 }

You can use this property of CLLocationManager: 您可以使用CLLocationManager的此属性:

@property(readonly, NS_NONATOMIC_IPHONEONLY) CLLocation *location;

The value of this property is nil if no location data has ever been retrieved, otherwise, this is where CoreLocation caches its data. 如果从未检索到任何位置数据,则此属性的值为nil ,否则,CoreLocation将在此处存储其数据。 Therefore, if you always want to start from scratch, simply check if this property is nil or not. 因此,如果您始终想从头开始,只需检查此属性是否为nil If it's nil , then you are ok; 如果是nil ,那么你还好; 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. 否则,您需要停止位置管理器并重新启动它:这将强制进行更新,这将导致缓存的值被刚触发的新值所覆盖。

See my answer here : How to remove cache of cllocation? 在这里查看我的答案: 如何删除cllocation缓存? about "how to remove cache of cllocation". 关于“如何删除cllocation缓存”。 this should solve your issue regarding the cache of the CLLocationManager. 这应该可以解决有关CLLocationManager缓存的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM