How to get the most accurate possible speed from GPS in Android

妖精的绣舞 提交于 2019-12-07 13:08:46

问题


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?


回答1:


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




回答2:


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.




回答3:


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 :)



来源:https://stackoverflow.com/questions/9767151/how-to-get-the-most-accurate-possible-speed-from-gps-in-android

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