5

How can I get an accurate speed from GPS in Android? Yes, I am aware of the location.getSpeed() method in the Location class. Problem is, the default implementation returns 0.0 as speed: apparently that is the default behavior.

What I'm currently doing, is as follows, consider location objects a and b, where a is taken first, b later:

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

(simplified for readability, original code deals with history ArrayList)

Problem is that this is somewhat inaccurate: under normal circumstances, the data points are so close to one another that the GPS inaccuracy really becomes an issue. Either I would need to reduce the update frequency or calculate the speed relative to a point further away. The former I don't want to do, as I want to get as high a frequency as possible, but perhaps I could filter the points to calculate speed against based on their distance to one another? The optimal solution, which I assumed the getSpeed() method would do, would be to calculate the speed against the GPS satellites themselves, thus getting a more accurate result. Am I using the getSpeed() wrong somehow?

Community
  • 1
  • 1
  • Hello Visa, I think you should include Accuracy parameter in distance, When we get lat,log from Location class, It not 100% accurate, Location class has method named "getAccuracy()". it return int - accuracy in meter. you should include this in calculation – Ketan Parmar Mar 19 '12 at 08:54
  • The emulator apparently always answers 0 as speed, but the real device should not. Do you have the same issue on the real device? – Stefan Mar 20 '12 at 08:21

3 Answers3

0

The emulator apparently always answers 0 as speed, but the real device should not. Do you have the same issue on the real device? – Stefan Mar 20 at 8:21

Stefan's answer was actually correct. Apparently the emulator does not give the speed, as that's not contained in the GPX file input as the testing data. So if you want to show speed, test on a real device and go for a jog, it'll work (for most devices).

Below are some thoughts for other methods of detecting speed, but not strictly relevant, but might be interesting if you're working with GPS.

Due to the relative inaccuracy of GPS, particularly at slow speeds or curvy roads the speed is hard to calculate: either the distance between data points is so short GPS inaccuracy comes to play, or so long it becomes inaccurate when not moving straight. Also, if the minimum distance between data points to calculate speed is long, at slow speeds the update interval becomes a problem. There are ways around this problem, such as using the getAccuracy() method to calculate minimum safe distance between data points and using it dynamically, filtering data points based on maximum acceleration and deceleration values, movement direction and so on. You can also calculate a rolling average to calm down the changes a little and get a pretty good idea of what's what.

The above methods may be useful also even if you don't calculate speed based on distance covered, as sometimes the GPS seems to return speed as 0, even when you're moving. I used acceleration/deceleration figures from F1 cars as filters :)

0

Since your keeping a history why not...

Get the current location and time

Find the speed between current and last ~10

Take an average of your results

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
0

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

Keep in mind this speed will be affected by any stops such as stop signs or stop lights. Sample as often as possible and discard any obvious outliers.