编写GPS记录应用程序~
我发现LocationManager
返回的Locations
的getSpeed()
方法返回值极不可靠。我正在使用LocationManager.GPS_PROVIDER
,通过onLocationChanged
提供的位置进行过滤以获得最佳准确性。即使在单位数的准确性水平下,返回的速度通常也非常高。当手机静止时,速度甚至高达每小时200英里(是的,我知道它以米/秒记录)。
我在两种不同型号的Android手机上测试相同的代码库,运行两个不同版本的操作系统,并看到相同的问题,所以我认为这是一个代码问题。
我错过了什么?我已经尝试了一段时间内对位置进行平均处理,但没有效果。难道我要根据旅行距离/时间计算自己的速度值吗?这将是令人失望的。
正如您所看到的,我没有做任何特殊的事情-即使在经过一些准确性过滤后,AverageSpeed
和_bestLocation.getSpeed()
仍然经常出现不合理的高速度,即使位置准确性很好。
public void onLocationChanged(Location location) {
if (location.getAccuracy() < 25f) {
_recentLocations.add(location);
if (_bestLocation == null || location.getAccuracy() <= _bestLocation.getAccuracy())
_bestLocation = location;
}
if ((_bestLocation != null && _bestLocation.getAccuracy() < 10f && _recentLocations.size() >= 10)
|| _recentLocations.size() >= 25)
{
int Count = 0;
float TotalSpeed = 0f;
float AverageSpeed = 0f;
for (int i = 0; i<_recentLocations.size(); i++) {
if (_recentLocations.get(i).hasSpeed()) {
Count++;
TotalSpeed += _recentLocations.get(i).getSpeed();
}
}
if (Count > 0)
AverageSpeed = TotalSpeed / Count;
}
}