简体   繁体   中英

How can I get a continous geolication from Xamarin.Forms?

I'm trying to get the Location of my device continuously using the Xamarin Essentials, but his behavior is quite strange. The GPS signal is activated and disable each time I'm using the GetLocationAsync method (is like a blinking mode).

This is my code:

Main Thread:

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);

...

If I compare this behavior with another app that use GPS location the just activate the GPS signal once and no more. What I doing wrong?

These are the results:

搜索 GPS

有源模块

As far as i know, Xamarin.Essentials doesn't have a location listener to detect location changes in the background. A solution would be using the the Geolocator Plugin from jamesmontemagno.

Here is an example on how to use it ( 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;
    });
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM