Maricris
Maricris

Reputation: 97

How to properly implement location changes listener to xamarin forms?

I have a listener that captures the location every 10 seconds or 100 meters or so. I am using

xam.plugin.geolocator

to implement the listener. My problem is the location listener is not working(meaning the changes in location were not capturing or saved in the location cache) when my application is minimized or the application is opened but the phone is locked.

Here is my code:

async Task StartListening()
    {
        if (!CrossGeolocator.Current.IsListening)
        {
            var defaultgpsaccuracy = Convert.ToDouble(Preferences.Get("gpsaccuracy", String.Empty, "private_prefs"));
            await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(10), defaultgpsaccuracy, false, new Plugin.Geolocator.Abstractions.ListenerSettings
            {
                ActivityType = Plugin.Geolocator.Abstractions.ActivityType.Other,
                AllowBackgroundUpdates = true,
                DeferLocationUpdates = true,
                DeferralDistanceMeters = 1,
                DeferralTime = TimeSpan.FromSeconds(1),
                ListenForSignificantChanges = true,
                PauseLocationUpdatesAutomatically = false
            });
        }
    }

I place this code in the first view/page of my application in my login.xaml.cs

Here are my questions:

  1. How can I implement the listener properly so that when the application minimized or the phone/device is locked it still captures the changes of location?
  2. What is the best GPS settings I need to capture the changes in location faster and accurately? Right now, my current settings are capturing the location every 10 seconds or 100 meters.

Upvotes: 1

Views: 1939

Answers (1)

LeRoy
LeRoy

Reputation: 4436

First you need to init StartListening then create event handlers for position changes and error handling

public Position CurrentPosition { get; set; } public event EventHandler PositionChanged;

Don't forget to init it in your constructor : CurrentPosition = new Position();

await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(20), 10, true); CrossGeolocator.Current.PositionChanged += PositionChanging; CrossGeolocator.Current.PositionError += PositionError;

Functions :

    `private void PositionChanging(object sender, PositionEventArgs e)
    {
        CurrentPosition = e.Position;
        if (PositionChanged != null)
        {
            PositionChanged(this, null);
        }
    }

    private void PositionError(object sender, PositionErrorEventArgs e)
    {
        Debug.WriteLine(e.Error);
    }`

You can now call PositionChanged when ever you want the latest position

Don't forget to stop listening :

   `public async Task StopListeningAsync()
    {
        if (!CrossGeolocator.Current.IsListening)
            return;

        await CrossGeolocator.Current.StopListeningAsync();

        CrossGeolocator.Current.PositionChanged -= PositionChanging;
        CrossGeolocator.Current.PositionError -= PositionError;
    }`

Upvotes: 1

Related Questions