Geolocation.GetLocationAsync() not working

Wei Wen 1,126 Reputation points
2021-07-07T16:33:12.933+00:00

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.

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

Accepted answer
  1. Wei Wen 1,126 Reputation points
    2021-07-07T17:31:21.013+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.