在 Windows Phone 应用程序中,我得到这样的地理位置:
Geoposition geoposition = await geolocator.GetGeopositionAsync(
maximumAge: TimeSpan.FromMinutes(5),
timeout: TimeSpan.FromSeconds(10));
如果我理解正确的话,maximumAge 意味着位置可以被缓存并且最多 5 分钟。
我可以使用这个“内置缓存”以便在我的应用程序启动时使用 GetGeopositionAsync(...) 和 maximumAge 例如2(分钟),只是为了在不实际使用地理位置的情况下获得新鲜的地理信息。
稍后在应用程序中,当我真正需要位置时,我再次调用 GetGeopositionAsync(...) 和 maximumAge 例如10(分钟)。现在我应该获取缓存值(如果它基于 maximumAge 仍然有效)?
这有什么意义吗?如果这按我预期的那样工作,我就不需要构建自己的地理定位缓存系统。
最佳答案
是的,没错。一篇 MSDN 博客文章 ( http://blogs.windows.com/windows_phone/b/wpdev/archive/2012/11/30/acquiring-a-single-geoposition-in-windows-phone-8.aspx ) 很好地解释了使用 MaximumAge
进行缓存:
MaximumAge
defines a limit of how old a cachedGeoposition
can be, from the time the operation starts, for it to be used by the app. By default, the request will not return a cached Geoposition. If your app can use cached positions for a brief period of time, this parameter also can be seen as the validity time span. The app can simplify its logic by letting theGeolocator
handle the caching.For example, if the app sets
MaximumAge
to 5 minutes, the Geolocator can satisfy that request with aGeoposition
that is as old as 5 minutes from the time the request is started, as long as that position meets the accuracy requirement specified by the app.Note that the
Geolocator
will always take the following sequential steps to satisfy a request:
The request is started.
Identify whether a cached
Geoposition
satisfies the age and desired accuracy, and if so, immediately return it.Trigger positioning technologies based on the desired accuracy.
Return a
Geoposition
when it meets the desired accuracy.When the timeout is reached, if no position is available yet, error out. If there is a position available, given that the framework follows a best-effort model, this
Geoposition
with a lower accuracy than desired by the app can be returned at this time. The app can decide whether the information is useful or to discard it.
关于c# - Windows Phone 8 Geolocator 缓存位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23973895/