Select to view content in your preferred language

GPS on Android not Working

1262
2
12-13-2018 10:25 AM
AndyWright
Frequent Contributor

I've got a Xamarin Forms application with some GPS functionality that has been working great for years.  Since we've upgraded to 100.4 our GPS functionality is not working on Android.  Still works great on iOS though.  Basically as soon as we set the IsEnabled property on the MapView.LocationDisplay to true it immediately resets itself to false.

We've tested this on two different Android devices, one with 6.0.1, and one with 7.1.1.  Doesn't work on either.  I've attached a repro solution here.

0 Kudos
2 Replies
MatveiStefarov
Esri Contributor

Hello, Andy!

The LocationDisplay component is powered by a LocationDataSource. When you set LocationDisplay.IsEnabled to true, it will try starting its data source. If for some reason it cannot be started successfully, this property will automatically reset to "false". To get more detail about the failure, you can use the LocationDataSource.StartAsync() method directly. Awaiting the Task will give you access to an Exception with details. For example:

private async void Button_Clicked(object sender, System.EventArgs e)
{
    var locationDataSource = map.LocationDisplay.DataSource;
    try
    {
        await locationDataSource.StartAsync();
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Could start LocationDataSource. Details: " + ex);
    }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

In this case, looks like LocationDataSource is failing to start due to a permission issue:

[0:] Could not access location. Details: Java.Lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
at Esri.ArcGISRuntime.Location.SystemLocationDataSource+<OnStartAsync>d__3.MoveNext () [0x0013a]

This will happen on Android 6.0+ because the OS no longer automatically grants certain permissions — apps have to make an additional API call to request them. You have several options for doing this in a Xamarin Forms app (e.g. by using the open-source PermissionsPlugin library or by calling Android APIs directly). After modifying this app to request location permission, I was able to display location:

I hope this helps!

- Matvei Stefarov

AndyWright
Frequent Contributor

Thanks Matvei.  This definitely helps.

0 Kudos