question

WeiWen-3421 avatar image
0 Votes"
WeiWen-3421 asked ShwetaDeshpande-7668 commented

Geolocation.GetLocationAsync() not working

I have this code:

var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
var currentLoc = await Geolocation.GetLocationAsync(request);
if (currentLoc?.Latitude != 0 && currentLoc?.Longitude != 0)
{
....
}

When execution reaches the second line, it skips it and also the rest of the code and jumps out of the entire function (the above is only part of a function). Location permission is already granted, and I didn't include it here. Anyone knows why this is happening? It is hard to debug because any exception catch branches are ignored, and no errors are thrown.

dotnet-xamarinforms
· 3
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.

Hi, @WeiWen-3421 Which platform did you face the issue? I create a basic demo to test the code, it works as expected. Please add a breakpoint to debug the code.

It is hard to debug because any exception catch branches are ignored

Is there any exception occured? Try using the following catch brances and add breakpoints to test. Or just remove the try-catch sentence to check it.

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
    }
}
0 Votes 0 ·

Hi @JarvanZhang-MSFT I found this issue on Android. I noticed that you used cancellation token and more catch branches. So I also added the cancellation token and other exception handling branches. Still the first time await Geolocation.GetLocationAsync(request, cts.Token) was reached, the function execution ended and brought me to the calling function. None of the exception handling branches were reached. I had to call the function the second time to get location information, just the same as in my solution.

0 Votes 0 ·
WeiWen-3421 avatar image WeiWen-3421 JeffWhattam-6305 ·

Sorry about the mixed IDs.

0 Votes 0 ·

1 Answer

WeiWen-3421 avatar image
0 Votes"
WeiWen-3421 answered ShwetaDeshpande-7668 commented

If I called the same function twice, on the second time Geolocation.GetLocationAsync() was invoked and it returned location. It looks like the timespan is not long enough. But even when I set it to 60 seconds, the first time it was still ignored. It is still the second time when it was invoked and returned the location. Don't know why. But as long as this works, it is still a solution.

· 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.

Can you please share the code?

0 Votes 0 ·