GPS acquisition with Cross Geolocator

木幡 弘文 1 Reputation point
2021-10-20T09:28:23.15+00:00

I want to write the acquired data once per second using CrossGeolocator, but sometimes I acquire the data twice per second.

public async Task StartListening()
{
    try
    {

        CrossGeolocator.Current.DesiredAccuracy = 1;
        await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 1, false);

        CrossGeolocator.Current.PositionChanged += PositionChanged;

    }
    catch
    {
    }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,661 Reputation points Microsoft Vendor
    2021-10-21T07:27:40.963+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I notice you set the minimumTime and minimumDistance at the same time, sometimes you acquire the data twice per second. it caused by the mini distance fired in this seconds. please change the value of minimumDistance to bigger value(such as minimumDistance to 50).

    Or You can use Xamarin.Essentials: Geolocation and Device.StartTimer to achieve the acquired data once per second like following code.

       public MainPage()  
               {  
                   InitializeComponent();  
                   
         
                   Device.StartTimer(TimeSpan.FromSeconds(1), () =>  
                   {  
                       GetCurrentLocation();  
                       return true;  
                   });  
         
                     
               }  
         
               private async void GetCurrentLocation()  
               {  
                   // throw new NotImplementedException();  
         
                   try  
                   {  
                       var location = await Geolocation.GetLastKnownLocationAsync();  
         
                       if (location != null)  
                       {  
                           Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");  
                       }  
                   }  
                   catch (FeatureNotSupportedException fnsEx)  
                   {  
                       // Handle not supported on device exception  
                   }  
                   catch (FeatureNotEnabledException fneEx)  
                   {  
                       // Handle not enabled on device exception  
                   }  
                   catch (PermissionException pEx)  
                   {  
                       // Handle permission exception  
                   }  
                   catch (Exception ex)  
                   {  
                       // Unable to get location  
                   }  
               }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.