question

38512626 avatar image
0 Votes"
38512626 asked LeonLu-MSFT edited

GPS acquisition with Cross Geolocator

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
     {
     }
 }
dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@38512626 May I know if you have got any chance to check my answer? I am glad to help if you have any other questions

0 Votes 0 ·