Android: distanceTo () invalid values

I am writing an application to track my route. I request GPS updates every minute and it works great. It shows me my exact point. When I want to calculate the distance between the current point and the previous one, it works fine, but from time to time it calculates the distance completely erroneously (I moved about 200 m, and it reconfigured me for more than 10 km). Does anyone know why this might happen?

Here is the function I'm using:

iRoute += myGPSLocation.distanceTo(prevLocation); 

Thanks in advance!

0
android gps location distance
Aug 22 '13 at 10:13
source share
2 answers

Stop using the google Location.distancebetween and location.distanceto functions. They do not work consistently.

Instead, use the direct formula to calculate the distance:

 double distance_between(Location l1, Location l2) { //float results[] = new float[1]; /* Doesn't work. returns inconsistent results Location.distanceBetween( l1.getLatitude(), l1.getLongitude(), l2.getLatitude(), l2.getLongitude(), results); */ double lat1=l1.getLatitude(); double lon1=l1.getLongitude(); double lat2=l2.getLatitude(); double lon2=l2.getLongitude(); double R = 6371; // km double dLat = (lat2-lat1)*Math.PI/180; double dLon = (lon2-lon1)*Math.PI/180; lat1 = lat1*Math.PI/180; lat2 = lat2*Math.PI/180; double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); double d = R * c * 1000; log_write("dist betn "+ d + " " + l1.getLatitude()+ " " + l1.getLongitude() + " " + l2.getLatitude() + " " + l2.getLongitude() ); return d; } 
+1
Jan 20 '14 at
source share

distanceTo () is working correctly.
The error is on your side, the most probable algorithmic one, for example, if there are no GPS fixes available and the phone occupies a location based on GSM-based cellular communication, this, of course, can be turned off by 1000 meters.

For your application, which probably wants to summarize the distance traveled, use only GPS fixes, do not use a different LocationProvider than GPS!

0
Aug 22 '13 at 15:02
source share



All Articles