Forum Xamarin.Forms

ON/OFF of GPS reception

Techno_DevelopTechno_Develop Member ✭✭✭

As shown in the code below, when you press (Start) (GPSStart_Clicked) or end (GPSStop_Clicked), you may not receive, or the reception may not end.

private void GPSStart_Clicked(Object sender, EventArgs e)
{
    _ = StartListening();
}

private void GPSStop_Clicked(Object sender, EventArgs e)
{
    _ = StopListening();
}

public async Task StartListening()
{
    if (CrossGeolocator.Current.IsListening)
        return;
    await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1000), 1, false);
    CrossGeolocator.Current.PositionChanged += PositionChanged;

}

public async Task StopListening()
{
    if (!CrossGeolocator.Current.IsListening)
        return;
    await CrossGeolocator.Current.StopListeningAsync();
    CrossGeolocator.Current.PositionChanged -= PositionChanged;
}

private void PositionChanged(object sender, PositionEventArgs e)
{
    ~~~~~~~~~~~~~~~~~~~~
}

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Try to add more settings like:

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

    Moreover, please notice that the event won't be triggered if the position isn't updated. This depends on the accuracy you set above. So it could happen that the event isn't called after listening.

Sign In or Register to comment.