停止返回缓存的 CLLocationManager 位置

发布于 2024-08-21 16:32:34 字数 136 浏览 6 评论 0原文

我想知道是否有某种方法可以使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪痕残 2024-08-28 16:32:34

你无法阻止它缓存,但过滤掉缓存的数据并不难。核心位置包括带有其位置的时间戳。将位置的时间戳与应用程序启动时保存的时间戳进行比较,您将能够辨别哪些位置是旧的(缓存的,在应用程序声明之前找到的)和哪些是新的。扔掉旧的。

位置时间戳是 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.

隱形的亼 2024-08-28 16:32:34
-(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 
    } 
 }
-(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 
    } 
 }
半衾梦 2024-08-28 16:32:34

您可以使用 CLLocationManager 的此属性:

@property(readonly, NS_NONATOMIC_IPHONEONLY) CLLocation *location;

如果从未检索过位置数据,则此属性的值为 nil,否则,这是 CoreLocation 缓存其数据的位置。因此,如果您总是想从头开始,只需检查此属性是否为 nil 即可。如果它是nil,那么就可以了;否则,您需要停止位置管理器并再次启动它:这将强制更新,这将导致缓存的值被刚刚触发的新值覆盖。

You can use this property of 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. Therefore, if you always want to start from scratch, simply check if this property is nil or not. If it's nil, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文