How can I calculate the cumulative distance travelled using the Android GPS?
I'm using Android Location services to listen to the location change events but simply finding the distanceTo from the last point to the new point and adding it to a running total tells me I've travelled a distance when I'm stationery.
public void onLocationChanged(Location newLocation) {
if (lastLocation != null)) {
distanceTravelled = distanceTravelled + lastLocation.distanceTo(newLocation);
Toast.makeText(this, distanceTravelled + "", Toast.LENGTH_SHORT).show();
}
lastLocation = newLocation;
}
I'm guessing I need to take into account the location accuracy of each point before adding it to the cumulative total. Is there an algorithm for this?
Thanks in advance!
There's a couple of ways to do this. One is to drop small movements. If you get a location update and the distance moved is less than R, ignore it. As R gets bigger you'll completely eliminate noise, but may eliminate real moves smaller than R as well. You can figure out an R that's good enough for your problem based on trial and error.
Another way would be to do something like a Kalman filter, which treats your position as a probability function. But that gets into a lot of complicated math, and has some of the same problems as the drop approach.
Another method would be to combine the data with accelerometer data to determine if you've actually started moving. Google does this in its activity detection code. The goal here would be to determine when you start and stop moving by seeing acceleromaeter spikes, and ignore small movements if not preceeded by a spike (include large movements still as that may be motion at a constant velocity).
Nothing will give you 100% accuracy of course. But you should be able to get pretty good results from these techniques.