Forum Xamarin.Forms

Xamarin Forms Geolocator - Issue with accurate tracking of location

I'm working on an application whick tracks the distance driven. I use James Montemagno's GeolocatorPlugin to handle the geolocation.

I'm able to get the current location and I'm able to start listening to location updates. The problem is, when listening to updates the event PositionChanged keeps firing and the positions changes. Even when I'm stationary. The changes in position are generally around 1km. When I do a test drive home, which is around 15 kilometers, the meter kan sometimes be a 200 kilometers. I've checked the calculations, that calculate the distance in kilometers for to GPS points and they seem to be correct.

Has somebody had similar issues, or does someone know what I'm doing wrong?

This is the code which starts the listening process.

var locator = CrossGeolocator.Current;

            if (!locator.IsListening)

                //Start listening to location updates
                await locator.StartListeningAsync(TimeSpan.FromSeconds(10), 0, true);

                locator.PositionChanged += PositionChanged;
                locator.PositionError += PositionError;

This method gets called when when the PositionChanged event is called

            public void UpdatePosition(Position newPos)
    //tracks the amount of method calls

                Distance += DistanceInKmBetweenEarthCoordinates(newPos.Latitude, newPos.Longitude, position .Latitude, position .Longitude);
                position = newPos;

These are the calculations to get the kilometers between two points

public double DistanceInKmBetweenEarthCoordinates(double lat1, double lon1, double lat2, double lon2)
            var earthRadiusKm = 6371;

            var dLat = DegreeToRadian(lat2 - lat1);
            var dLon = DegreeToRadian(lon2 - lon1);

            lat1 = DegreeToRadian(lat1);
            lat2 = DegreeToRadian(lat2);

            var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                    Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
            var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            return earthRadiusKm * c;
        private double DegreeToRadian(double angle)
            return angle * Math.PI / 180.0;

Best Answer


Sign In or Register to comment.