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) { //CrossGeolocator //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 MethodCalled++; 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; }
@AngeloBrouns - I suggest creating a log of all of the longitude/latitude pairs received during a journey so that you can examine the data afterwards to look for values that are clearly incorrect. If you can identify how those positions were generated, or limit the possibilities to one (i.e. GPS), you might be able to identify why dirty data is being produced. For example, if position information is only obtained using GPS it may be that one or more satellites are being blocked at various points in the journey by buildings, landscape or even larger vehicles. If that's the problem, you will need to generate an algorithm that discards positions that are likely to be wrong. If non-GPS information is being used, it may be that you have moved to an area with a low density of mobile phone towers, and so the accuracy diminishes. There are various possibilities, but it's a data analysis job to track down the problem.
Answers
@AngeloBrouns - I suggest creating a log of all of the longitude/latitude pairs received during a journey so that you can examine the data afterwards to look for values that are clearly incorrect. If you can identify how those positions were generated, or limit the possibilities to one (i.e. GPS), you might be able to identify why dirty data is being produced. For example, if position information is only obtained using GPS it may be that one or more satellites are being blocked at various points in the journey by buildings, landscape or even larger vehicles. If that's the problem, you will need to generate an algorithm that discards positions that are likely to be wrong. If non-GPS information is being used, it may be that you have moved to an area with a low density of mobile phone towers, and so the accuracy diminishes. There are various possibilities, but it's a data analysis job to track down the problem.