简体   繁体   English

如何在Android中从GPS获得最准确的速度

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

How can I get an accurate speed from GPS in Android? 如何在Android中获得GPS的准确速度? Yes, I am aware of the location.getSpeed() method in the Location class. 是的,我知道Location类中的location.getSpeed()方法。 Problem is, the default implementation returns 0.0 as speed: apparently that is the default behavior . 问题是,默认实现返回0.0作为速度:显然这是默认行为

What I'm currently doing, is as follows, consider location objects a and b, where a is taken first, b later: 我目前正在做的事情如下,考虑位置对象a和b,首先采用a,b后面:

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

(simplified for readability, original code deals with history ArrayList ) (为便于阅读而简化,原始代码处理历史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. 问题是这有些不准确:在正常情况下,数据点彼此非常接近,以至于GPS不准确性确实成为一个问题。 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. 我假设getSpeed()方法的最佳解决方案是计算GPS卫星本身的速度,从而获得更准确的结果。 Am I using the getSpeed() wrong somehow? getSpeed()以某种方式使用了getSpeed()错误?

The emulator apparently always answers 0 as speed, but the real device should not. 模拟器显然总是以速度回答0,但真正的设备不应该。 Do you have the same issue on the real device? 你在真实设备上有同样的问题吗? – Stefan Mar 20 at 8:21 - Stefan 3月20日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. 显然,仿真器没有提供速度,因为GPX文件输入中没有包含测试数据。 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. 以下是其他检测速度的方法的一些想法,但不是严格相关的,但如果你正在使用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. 由于GPS的相对不准确性,特别是在低速或弯曲的道路上,速度很难计算:数据点之间的距离是如此短,GPS不准确性发挥作用,或者长时间不变直线时变得不准确。 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. 有一些解决此问题的方法,例如使用getAccuracy()方法计算数据点之间的最小安全距离并动态使用它,根据最大加速度和减速度值,移动方向等过滤数据点。 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. 即使您不根据所覆盖的距离计算速度,上述方法也可能有用,因为有时GPS似乎将速度返回为0,即使您正在移动。 I used acceleration/deceleration figures from F1 cars as filters :) 我使用F1赛车的加速/减速数据作为滤波器:)

Since your keeping a history why not... 既然你保持着历史,为什么不......

Get the current location and time 获取当前位置和时间

Find the speed between current and last ~10 找出当前和最后10之间的速度

Take an average of your results 取平均结果

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(). 您可以通过调用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. 尽可能经常采样并丢弃任何明显的异常值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM