Bug report best practices: Submitting Issues
Android phones failes to fetch location sometimes. No errors are thrown and the awaited method call to GetLocationAsync() never returns.
When debugging the source code I find that the call to GetBestProvider in Geolocation.android.cs/PlatformLocationAsync method returns the passive location provider when the GPS provider is not available. This provider relies on other applications to fetch the location and does not actually initiate any request. I'm not a 100% sure that this is the cause, but relying on the passive provider to fetch could probably cause this if we are not running any other applications that are actively requesting locations themselves at the same time.
https://developer.android.com/reference/android/location/LocationManager#PASSIVE_PROVIDER
Should return that no location could be found
Never returns
I would also add that I am experiencing the same behavior on iOS, and not just Android. When I call Geolocation.GetLastKnownLocationAsync() It never returns and my application hangs when location services are disabled. Since i cant add a timeout to it like the GetLocationAsync I cant get around the issue.
I am testing on Version 0.10.0 on iOS 11.2
@asfelicetti just a thought, I was having similar issues on iOS and it turned out to be that I was accidentally launching the permission check from a background thread, but it has to be on the UI thread otherwise exceptions or the initial location aren't propagated back to your code.
See this issue for details: https://github.com/xamarin/Essentials/issues/373
I renamed it. I will be adding some checks for 0.11.0 to see if it is disabled on the device.
Additionally, we wont check permissions if not on the UI in 0.11.0
GetLastKnownLocation doesn't query any of the services and would work even if location services are off. Seems like @asfelicetti that maybe a permission issue perhaps. Can you give me your sample code how you are using it.
@jamesmontemagno
I have a viewmodel in my app and one of the functions when i am loading data calls the get lastknown location method to calculate miles from a list of addresses. I loop through the different addresses to calculate miles from the users current location. The first time the method is called after I disable location services it does indeed throw a permission exception as follow ({Xamarin.Essentials.PermissionException: LocationWhenInUse was not granted)}. Subsequent calls to the exact method doesnt throw the same permission exception it just hangs on the getlastknownlocationasync method. My info.plist has all of the requisite keys as shown below.
Here is the function
public async static Task
{
try
{
var currentLocation = await Geolocation.GetLastKnownLocationAsync();
if (currentLocation != null)
{
Xamarin.Essentials.Location officeLocation = new Xamarin.Essentials.Location(coordinate.Latitude, coordinate.Longitude);
var distance = Xamarin.Essentials.Location.CalculateDistance(currentLocation, officeLocation, DistanceUnits.Miles);
return Math.Round(distance, 1);
}
}
catch (FeatureNotSupportedException fnsEx)
{
// Handle not supported on device exception
}
catch (PermissionException pEx)
{
// Handle permission exception
}
catch (Exception ex)
{
// Unable to get location
}
return 0;
}
info.plist
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access location when open.</string>
Got it. I will see what is happening here.
With my PR this will be fixed and we will throw a "FeatureNotEnabledException"
This still happens for me on Android for Xamarin Essentials GetLocationAsync never returns when I have location off.
This still happens for me on Android for Xamarin Essentials GetLocationAsync never returns when I have location off too.
Also still happening for me an Android.
@jamesmontemagno would you consider re-opening this for investigation?
+1
So....
We do look at the providers available:
var locationManager = Platform.LocationManager;
var enabledProviders = locationManager.GetProviders(true);
var hasProviders = enabledProviders.Any(p => !ignoredProviders.Contains(p));
if (!hasProviders)
throw new FeatureNotEnabledException("Location services are not enabled on device.");
static readonly string[] ignoredProviders = new string[] { LocationManager.PassiveProvider, "local_database" };
So, the question is.... what providers are showing up on your device....
Happening for me too on both iOS and Android
Most helpful comment
This still happens for me on Android for Xamarin Essentials GetLocationAsync never returns when I have location off.