Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Geolocation.GetLocationAsync Android

$
0
0

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


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>