Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

GetLocationAsync with time out get same locations

I request location every 1 min, and set a time out for request minor of 1 min, because dont want a request not finish and set other request.
But the problem i see is i get the same latitude and longitude in subsecuences resquests, the execution was about 1 hour in travel

var ts = new TimeSpan();
ts = TimeSpan.FromMilliseconds(59000);
var request = new GeolocationRequest(GeolocationAccuracy.Medium, ts);
var location = await Geolocation.GetLocationAsync(request);

I save a local log, and for example:

10/24/2020 2:59:16 p. m.

OnTimeEvent

Init resquest location----- 10/24/2020 2:58:16 p. m.
Latitude: -25.3914806, Longitude: -57.1473436, Altitude: 221.300003051758 ,Speed:

End request location----- 10/24/2020 2:59:16 p. m.

10/24/2020 2:56:01 p. m.

OnTimeEvent

Init resquest location----- 10/24/2020 2:52:46 p. m.
Latitude: -25.3914806, Longitude: -57.1473436, Altitude: 221.300003051758 ,Speed:
End----- 10/24/2020 2:56:01 p. m.

My question is, the time out cause get the same location? the execution ocurrs in background that influend in the request?
Or what its happening?

Some one have the same problem?

Gratefull for any comment or help.

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    When we check the official document Using Geolocation, we will find:

    To query the current device's location coordinates, the GetLocationAsync can be used. It is best to pass in a full GeolocationRequest and CancellationToken since it may take some time to get the device's location.

    So you can try to pass in a full GeolocationRequest and CancellationToken.

    The sample code is

    CancellationTokenSource cts;
    
    async Task GetCurrentLocation()
    {
        try
        {
            var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
            cts = new CancellationTokenSource();
            var location = await Geolocation.GetLocationAsync(request, cts.Token);
    
            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
        }
    }
    
    protected override void OnDisappearing()
    {
        if (cts != null && !cts.IsCancellationRequested)
            cts.Cancel();
        base.OnDisappearing();
    }
    
  • ciullosanchezciullosanchez Member ✭✭

    Hello, thanks for reply, to understand, if i only set the time out, the api get me the last location?
    In the example if the app goes to background the request is cancel, rigth?

  • ciullosanchezciullosanchez Member ✭✭

    I dont try the function GetLastKnewLocation, but i test and see on my data when its cancel or time out, the data on location is the same every time.
    It's ok.
    Thanks

Sign In or Register to comment.