位置缓存如何工作?

时间:2019-07-09 06:26:14

标签: xamarin xamarin.forms xamarin.android geolocation

我有一个应用程序,每当用户填写表格时,该应用程序都会获取设备的GPS位置。我的问题是捕获GPS位置花费的时间太长。捕获GPS大约需要40到60秒。我正在使用 jamesmontemagno的Geolocator 插件。

GPS参数:
精度:100米
超时:1分钟

这是我现在正在使用的代码:

var defaultgpsaccuracy = Convert.ToDouble(Preferences.Get("gpsaccuracy", String.Empty, "private_prefs"));
var defaultgpstimeout = Convert.ToDouble(Preferences.Get("gpstimeout", String.Empty, "private_prefs"));

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

position = await locator.GetLastKnownLocationAsync();

if (position != null)
{
     string location = position.Latitude + "," + position.Longitude;
     lblStartLocation.Text = location;
}
else
{
     position = await locator.GetPositionAsync(TimeSpan.FromMinutes(defaultgpstimeout), null, false);

     string location = position.Latitude + "," + position.Longitude;
     lblStartLocation.Text = location;
}

这些是我的问题:

  1. 我使用了 locator.GetLastKnownLocationAsync(); 位置缓存刷新之前需要多长时间?
  2. 位置更改时,最近的已知位置是否会刷新
  3. 当设备超出精度范围(例如精度为100米)时,位置会刷新吗?如果设备位于最后一个已知位置的100米范围之外,位置缓存是否会刷新?

1 个答案:

答案 0 :(得分:0)

我强烈建议您通过the documentation查看该特定插件,James Montemagno是一位知名且受人尊敬的开发人员,使用我的Microsoft开发Xamarin框架,因此他的插件,扩展和工具包往往已进行了高度优化用于跨平台应用程序。

从文档中可以明显看出,尝试获取最后一个“已知”位置会查看内部缓存的位置数据集,并不一定针对近实时查询进行了优化。但是,它可以用来减少您在应用中必须执行的实际位置查询的次数。

链接文档中的完整代码段如下:

public async Task<Position> GetCurrentLocation()
{
   public static async Task<Position> GetCurrentPosition()
    {
            Position position = null;
            try
            {
                    var locator = CrossGeolocator.Current;
                    locator.DesiredAccuracy = 100;

                    position = await locator.GetLastKnownLocationAsync();

                    if (position != null)
                    {
                            //got a cahched position, so let's use it.
                            return position;
                    }

                    if (!locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled)
                    {
                            //not available or enabled
                            return null;
                    }

                    position = await locator.GetPositionAsync(TimeSpan.FromSeconds(20), null, true);

            }
            catch (Exception ex)
            {
                    Debug.WriteLine("Unable to get location: " + ex);
            }

            if (position == null)
                    return null;

            var output = string.Format("Time: {0} \nLat: {1} \nLong: {2} \nAltitude: {3} \nAltitude Accuracy: {4} \nAccuracy: {5} \nHeading: {6} \nSpeed: {7}",
                    position.Timestamp, position.Latitude, position.Longitude,
                    position.Altitude, position.AltitudeAccuracy, position.Accuracy, position.Heading, position.Speed);

            Debug.WriteLine(output);

            return position;
    }
}

此方法旨在遵循位置检索的分层模式,它从最后一个“已知”位置开始并在必要时进行查询,您还可以进一步执行此操作,并在超时时添加超时以检查缓存的位置你想要的。

关于刷新数据的频率,我将带您到documentation section titled 'Background Updates'

James以驾驶应用程序为例,介绍了该应用程序的工作方式。刷新在Android和iOS上的处理方式有所不同,但这是有关Android的代码段:

  

为此,您需要集成一个前台服务,   订阅位置更改,并且用户界面绑定到该位置。请   仔细阅读Xamarin.Android Services documentation

在他使用的代码示例中,他向您展示了如何创建一个“侦听器”,该侦听器将定期检查位置的变化。根据应用程序的目的,这可能更适合您要尝试的操作。

相关问题