【问题标题】:How to make geo location retrieval process faster in UWP?如何在 UWP 中加快地理位置检索过程?
【发布时间】:2018-02-01 11:50:26
【问题描述】:

我正在使用 Geolocator 类在 UWP 应用中查找设备的当前位置。位置检索过程在我的计算中运行得非常快。但是当我尝试在真实设备上运行相同的应用程序时,设备检索过程大约需要 30 秒。 我正在使用以下代码 sn-p:

var accessStatus = await Geolocator.RequestAccessAsync();
if (accessStatus == GeolocationAccessStatus.Allowed)
{
    Geolocator geolocator = new Geolocator
    {
        DesiredAccuracyInMeters = 500,
        DesiredAccuracy = PositionAccuracy.High
    };

    Geoposition pos = await geolocator.GetGeopositionAsync()
}

如何在我的设备中加快此过程?

已尝试将 DesiredAccuracyInMeters 值增加到 2000,但找不到任何改进。提前致谢。

【问题讨论】:

  • DesiredAccuracy = PositionAccuracy.High 怎么样?尝试将其设置为Default。根据documentation,它似乎试图涉及许多资源来获取精确位置 => 可能会很慢。

标签: c# performance uwp geolocation location


【解决方案1】:

如果你检查documentation,你可以看到当你同时设置DesiredAccuracy和DesiredAccuracyInMeters时,最后一组优先:

当 DesiredAccuracyInMeters 和 DesiredAccuracy 均未设置时,您的应用将使用 500 米的精度设置(对应于默认的 DesiredAccuracy 设置)。将 DesiredAccuracy 设置为 Default 或 High 会间接地将 DesiredAccuracyInMeters 分别设置为 500 或 10 米。当您的应用同时设置 DesiredAccuracy 和 DesiredAccuracyInMeters 时,您的应用将使用最后设置的精度值。

因此,因为您将DesiredAccuracy 设置为High,您实际上是在覆盖米设置。为加快搜索速度,请勿设置High 精度,仅设置米值。

【讨论】:

  • 谢谢。我只是错过了文档部分。
【解决方案2】:

我将补充马丁的问题,您应该先使用缓存的位置,然后使用GetPositionAsync,这样您应该可以更快地定位用户:

var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 500;

//Check if we have a cached position
var loc = await locator.GetLastKnownLocationAsync ();
if ( loc != null )
{
    CurrentPosition = new Position (loc.Latitude, loc.Longitude);
}

if ( !locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled )
{
    return;
}

//and if not we get a new one
var def = await locator.GetPositionAsync (TimeSpan.FromSeconds (10), null, true);
CurrentPosition = new Position (def.Latitude, def.Longitude);

【讨论】:

  • 我认为这种方法需要 GeoLocator 插件,对吧?
猜你喜欢
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
  • 2017-08-11
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多