【问题标题】:How can I get a continous geolication from Xamarin.Forms?如何从 Xamarin.Forms 获得连续的地理位置?
【发布时间】:2019-10-21 15:51:08
【问题描述】:

我正在尝试使用 Xamarin Essentials 连续获取设备的位置,但他的行为很奇怪。每次我使用 GetLocationAsync 方法(就像闪烁模式)时,GPS 信号都会被激活和禁用。

这是我的代码:

主线程:

Xamarin.Forms.Device.StartTimer(TimeSpan.FromMilliseconds(gpsFrequencyRead, () =>
        {
            Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
            {
                if (!isBusy)
                {
                    isBusy = true;
                    await MainMethodProcess();
                    isBusy = false;
                }
            });

            return activeMainTimer;

        });

MainMethodProcess:

...

Location location = new Location();

location = await Geolocation.GetLocationAsync(request);

...

如果我将此行为与另一个使用 GPS 定位的应用程序进行比较,则只需激活一次 GPS 信号即可。我做错了什么?

这些是结果:

【问题讨论】:

  • 您使用的是 Xamarin.Essentials 对吗?我认为 Essentials 没有位置变化的监听器,但 CrossGeolocator 有!我将发布一个带有一些代码+源代码的anwser

标签: c# xamarin xamarin.forms geolocation location


【解决方案1】:

据我所知,Xamarin.Essentials 没有位置监听器来检测后台的位置变化。 一个解决方案是使用来自 jamesmontemagno 的 Geolocator Plugin

这是一个如何使用它的示例 (source)

async Task StartListening()
{
    await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true, new Plugin.Geolocator.Abstractions.ListenerSettings
    {
        ActivityType = 
        Plugin.Geolocator.Abstractions.ActivityType.AutomotiveNavigation,
        AllowBackgroundUpdates = true,
        DeferLocationUpdates = true,
        DeferralDistanceMeters = 1,
        DeferralTime = TimeSpan.FromSeconds(1),
        ListenForSignificantChanges = true,
        PauseLocationUpdatesAutomatically = false
    });
    CrossGeolocator.Current.PositionChanged += Current_PositionChanged;
}

private void Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
{
    Device.BeginInvokeOnMainThread(() =>
    {
        var Position = e.Position;
    });
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    相关资源
    最近更新 更多