Forum Xamarin.Forms

Geolocation.GetLocationAsync Android

Hi All,

I'm having a random weird issue with Geolocation.GetLocationAsync.

When I am calling var getLastKnownLocation = await geoLocation.GetLastKnownLocation(); within an action on some devices, it constantly hangs on Geolocation.GetLocationAsync(request); without anytime or Exception and doesnt move!

One device this does this on is a Huawei P30 Pro, Android Version 9.

My code is below.

    public async Task<LocationViewModel> GetLastKnownLocation()
    {
        try
        {
            var mn = new LocationViewModel();

            var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(5));

            var location = await Geolocation.GetLocationAsync(request);

            if (location != null)
            {
                mn.Latitude = location.Latitude.ToString();
                mn.Longitude = location.Longitude.ToString();

                return mn;
            }

            return new LocationViewModel()
            {
                Longitude = "",
                Latitude = ""
            };              
        }
        catch (FeatureNotSupportedException fnsEx)
        {
            return new LocationViewModel()
            {
                Longitude = "",
                Latitude = ""
            };
        }
        catch (FeatureNotEnabledException fneEx)
        {
            return new LocationViewModel()
            {
                Longitude = "",
                Latitude = ""
            };
        }
        catch (PermissionException pEx)
        {
            return new LocationViewModel()
            {
                Longitude = "",
                Latitude = ""
            };
        }
        catch (Exception ex)
        {
            return new LocationViewModel()
            {
                Longitude = "",
                Latitude = ""
            };
        }
    }

Thanks,
Dane

Posts

  • JohnHardmanJohnHardman GBUniversity admin

    @""DaneMarshall.5776"

    I don't use Xamarin.Essentials for Geolocation, so don't know whether or not it does this for you - do you check that permission has been granted to do a location request before making the request?

  • jddjdd USMember ✭✭✭

    Same issue,
    It used to work and know does not work anymore
    The App gets stuck on the line:
    var location = await Geolocation.GetLocationAsync(request);
    the App freezes.
    The geolocation is authorized on the device and on the VS Emulator as well.
    I use Xamarin.Essentials 1.0.1

  • jddjdd USMember ✭✭✭

    Same issue with Xamarin.Essentials 1.2.0

  • ChurlieChurlie UGMember
    edited May 2020

    Make sure you have these permissions in your manifest file
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-feature android:name="android.hardware.location" android:required="false" /> <uses-feature android:name="android.hardware.location.gps" android:required="false" /> <uses-feature android:name="android.hardware.location.network" android:required="false" />

  • sisaackssisaacks Member ✭✭✭

    I have the same code as the guy above. What is very strange is the first time I make the call......Altitude is never returned. I press the button again and it is returned. I dont understand why altitude is never returned on the first call

Sign In or Register to comment.