What is the best way to calculate distance travelled over a period of time?
I am trying to calculate the distance elapsed over a period of time in an app for the Android platform.
What is the best way to do this? At the moment, I am implementing LocationListener interface. I am overriding the method onLocationChangedI am currently doing something like this:
public void onLocationChanged(Location location) {
Log.d("onLocationChanged", "onLocationChanged");
old_location.set(new_location);
new_location.set(location);
totalDistanceElapsed += new_location.distanceTo(old_location);
}
So I am adding the distance from the old location point to the new location point ever time "onLocationChang开发者_运维百科ed" is called. Is this correct? Do I need to do anything else? Will this be accurate? If not, how can I make it more accurate? thanksThe trivial way to determine distance travelled over time would be to obtain fixes every second, and read and record the speed reported for each fix (in meters per second). Then your distance travelled is the sum of all the speeds over the period of time elapsed.
If you want to avoid counting "jitter" as movement (i.e. if the fixes are wandering about slowly because of GPS fix inaccuracies, but the device is actually stationary) you can set a velocity threshold, e.g. by ignoring speeds below a certain level, or if the heading is changing rapidly between fixes. These thresholds will probably depend on the GPS device used, and some devices have this kind of thing built in already (i.e. if the speed is too low they report 0 m/s).
It really does depend on the device, however. For examply, my early model Samsung Galaxy I9000 wanders about in a 20 meter radius area even in clear sky conditions at about half a meter per second, so if it sits stationary in a field for ten minutes the above algorithm would report it as having travelled 300m.
精彩评论