What is the best way to calculate distance travelled over a period of time?

梦想的初衷 提交于 2019-12-05 06:29:04

问题


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 onLocationChanged

I 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 "onLocationChanged" 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? thanks


回答1:


The 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.



来源:https://stackoverflow.com/questions/6732040/what-is-the-best-way-to-calculate-distance-travelled-over-a-period-of-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!