How to get the position using Geolokator correctly?

How to get the position using Geolokator correctly? When I use GetPositionAsync method, it returns position with 0. In this moment in phone GPS icon appears, and if after 3 second I call GetPositionAsync again I get current position.

Main project:

var coord = DependencyService.Get<IGPSLocation>().GetCoordinates();

Android project:

` public class GPSLocation_Android : Java.Lang.Object, IGPSLocation
{
Geolocator locator;
Position position = new Position();
GPSCoordinates coordinates;

    public GPSCoordinates GetCoordinates()
    {
        GetPosition();

        coordinates = new GPSCoordinates()
        {
            Lat = position.Latitude,
            Lng = position.Longitude
        };

        return coordinates;
    }

    async void GetPosition()
    {
        try
        {
            locator = new Geolocator(Forms.Context) { DesiredAccuracy = 50 };
            if (locator.IsListening != true)
                locator.StartListening(minTime: 1000, minDistance: 0);
            position = await locator.GetPositionAsync(5000);
        }
        catch { }
    }
}

`

Sign In or Register to comment.