Xamarin.forms Get location info return TaskCancelException

HC 1 Reputation point
2021-10-06T01:37:19.273+00:00

I am encountering a problem is that the system will throw the TaskCancelException back when I am trying to get the location info. At the time, I try to open Google map(someone answered it can be solved by opening Google map) without connecting to Internet, and go back to APP to try again to get the location. The result is still give me back the exception. Then, I make the smartphone to connect to the Internet first, and then open Google map again, and then go back to APP to get the location. I can get the location info now!!!

The problem of not getting the gps location is not happened in every smartphone. The function of getting location info in the APP works fine in other phones(about 10 phones), but occurred in the Vivo V2050(with Android11).

Anyone know about this problem? How to fix it?

try
{
var locator=CrossGeolocator.Current;
var position = await locator.GetPositionAsync(timeoutMillseconds: 10000);
var long=position.Longitude;
}
catch (TaskCancelException ex)
{
}

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