Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin.Essentials Geolocation doesn't work in Android 9

Hello,

I'm trying get my device current position though Xamarin.essentials.

I'm using the Microsoft documentation's code:

try
{
    var request = new GeolocationRequest(GeolocationAccuracy.Medium);
    var location = await Geolocation.GetLocationAsync(request);

    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
}

This code works fine on Android 8 and iOS 12 but on Android 9 I never get the location.

Also I've used other method, in witch you can configure a time out for get the location, but on Android 9, when time expire, location is null.

Has anyone else had this issue? Any idea what is happening?

Thanks

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Have you added the permissions as the documentation said: https://docs.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=android?
    I've tried it on the Android 9.0 emulator the location could be obtained successfully.
    Did it fail on your particular device or each testing devices?
    Moreover, have you tried the GetLastKnownLocationAsync? It will be faster than doing a full query.

  • nnovalbosnnovalbos Member ✭✭

    Hello @LandLu ,

    I added permissions.

    I have this problem with a real device. On a emulator I have obtained location successfully.

    I will try to get more physical devices with Android 9, to see if the result is the same or is something of this device

    Thanks

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Are you getting an exception or does the method just return null?

  • nnovalbosnnovalbos Member ✭✭

    Hello @JoeManke

    The method return null if I put time out in the GeolocationRequest.

    If I don't put timeout, it stays waiting indefinitely

  • LandLuLandLu Member, Xamarin Team Xamurai

    It seems your device refuses to return the location information.
    Can other location apps work on this particular device?
    You'd better try other devices to test that.

  • nnovalbosnnovalbos Member ✭✭

    Hi,

    Yes, the device refused to return the location information. I don´t know why, because location is enabled in the device.

    But when I used google maps (first time on device), google maps ask me for location permissions, I accepted and then when i go back to my app, this feature works....

    I'm confusing... i will try reset the device for reproduce this situation

    Thanks for all.

  • LandLuLandLu Member, Xamarin Team Xamurai

    @nnovalbos It seems your application hasn't turned it on. Or there's something with it on your device.
    Have you checked it on other devices or emulators?

  • nnovalbosnnovalbos Member ✭✭

    @LandLu On Emulators it works perfectly.
    I will try get another device with Android 9. When I have done the test, I will post my results

    Thanks

  • TepcoTepco Member ✭✭

    After much playing around I got Geolocation to work on my smartphone (I had to change the target platform to Android 8.0) but I note, with dismay, that Android is only accurate to within 100 meters. I want to measure shorter distances than that. Is there another option short of relying exclusively on IPhones or Windows devices?

Sign In or Register to comment.