Forum Xamarin.Forms

Can't obtain location

FrankGuerraFrankGuerra USMember ✭✭
edited February 2018 in Xamarin.Forms

As title says guys can't get my device location with the method i implemented for it, this is the code i'm using for this:

public class Geolocation
{        
        private readonly LocationManager _locationManager;

        public Geolocation()
        {
            _locationManager = Forms.Context.GetSystemService(Context.LocationService) as LocationManager;
        }

        public Task<double> GetLocation()
        {

            var provider = _locationManager.GetBestProvider(new Criteria() { Accuracy = Accuracy.Fine }, true);
            var location = _locationManager.GetLastKnownLocation(provider);

            if(location == null) return null;

            var result = location.Latitude;

            return result;
        }

}

so the thing here is that the method sometimes works sometimes not, for those wondering why i'm not using James Montemagno GeolocatorPlugin is because there is a component i can't update in VS 2015 sadly, and have a lot of walls in my work to install components so.

returning to what matters can you guys help me with that piece of code? suggestions, comments are welcome, want to get this working right.
i'm targeting API 23 (6.0) if that's important, and i obviusly know that geolocation won't work in emulators so i'm testing it in my physical device.
thx for those that can help!!

Sign In or Register to comment.