Question

I am working on an application where I need to get the speed of a car. To get the speed, I know I can use something like double speed =locationB.getSpeed();` however when I am testing, the speed varies between 0.0 and 40 km/h when I am just sitting right behind my laptop not moving at all. In the car, the speed actually comes close to the cars speed, so that shouldn't be a problem.

What would be the best way to check if the device is really moving? I've already tried to get the distance between locationA and locationB and use that with the time it took to get the 2 locations, to get the speed.

double distance = locationA.distanceTo(locationB);
double speed = (distance / time) * 3600 / 1000;

However this seems to be not stable at all, like the getSpeed() method.

Is there a way to only display the speed if the device is moving? And would it be reliable?

Any help is appreciated,

Thanks.

Was it helpful?

Solution

Check the horicontal accuracy attribute of Location.
If it is under 30m you can ignore the location.

If you are sitting on your laptop and get speed = 40km/h (which I never saw in good GPS devices), then look what the hor. accuracy is. It probably is much over 30m.

In GPS based systems, never ever calculate the speed by positional change in time, just use the location.getSpeed(). The reason is that the GPS chip internally calculates the speed via physical doppler effect, not via positional change. While standing still, or at very low speeds this does not work well, so you have to filter out very low speeds, and bad gps signal. (via horicontal accuracy estimate)

OTHER TIPS

I think you should limit the distance between A and B to be a minimum length. Small distances will introduce more error into your speed calculations.

Boolean moving - false;
double distance = locationA.distanceTo(locationB);
double speed = (distance / time) * 3600 / 1000;
if (distance > SOME_THRESHOLD) {
   moving = true
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top