# How to Get the Most Accurate Possible Speed from GPS in Android

How can I get accurate speed from GPS in Android? Yes, I know the method `location.getSpeed()`

in the class `Location`

. The problem is that the default implementation returns 0.0 as speed: apparently this is the default behavior .

What I am currently doing is this: Consider location objects a and b, where a is taken first, b is taken later:

`a.distanceTo(b)/(b.getTime()-a.getTime());`

(simplified for readability, source code deals with history `ArrayList`

)

The problem is that this is somewhat inaccurate: under normal circumstances, the data points are so close together that GPS inaccuracy really becomes an issue. Either I will need to decrease the refresh rate, or calculate the speed relative to a point further away. First thing I don’t want to do as I want to get the highest frequency possible, but perhaps I could filter the points to compute velocity versus their distance to each other? The optimal solution, which I assumed the method `getSpeed()`

would do, would be to calculate the speed compared to GPS satellites, thus getting a more accurate result. Am I using it wrong somehow `getSpeed()`

?

source to share

The emulator seems to always respond with 0 as speed, but the real device doesn't. Do you have the same problem on a real device? - Stefan March 20 at 8:21

Stefan's answer was indeed correct. Apparently the emulator does not give speed like the one that is not included in the input of the GPX file as test data. So if you want to show your speed, test on a real device and go for a run, it will work (for most devices).

Below are some thoughts on other methods of speed detection, but not required, but might be interesting if you are working with GPS.

Due to the relative inaccuracy of GPS, especially at slow speeds or winding roads, the speed is difficult to calculate: either the distance between data points is so short that GPS inaccuracy comes into play, or it takes so long to become inaccurate if not moving straight. the distance between data points for speed calculation is long, at slow speeds the update interval becomes a problem. There are ways to solve this problem, such as using the getAccuracy () method to calculate the minimum safe distance between data points and dynamically using it, filtering data points based on maximum acceleration and deceleration values, direction of travel, etc. You can also calculate a moving average,to calm down the changes a bit and get a pretty good idea of what.

The above methods can be helpful even if you are not calculating the speed based on the distance traveled, as sometimes the GPS seems to return the speed as 0 even when you are moving. I used acceleration / deceleration numbers from F1 cars as filters :)

source to share

Use the formula you provided to determine the average speed, but make sure your two points are in a straight line. You could see if the user is traveling in the same direction by calling Location.getBearing (). If it's close enough, you can assume they were traveling in a straight line. If not just discard the result.

Remember that this speed will be affected by any stops such as brake lights or brake lights. Sample as often as possible and discard any obvious outliers.

source to share