Writing a GPS logging application~
I'm finding the values returned by the getSpeed()
method on Locations
reported by LocationManager
are massively unreliable. I'm using LocationManager.GPS_PROVIDER
, filtering the Locations provided through onLocationChanged
for best accuracy. Even at single digit accuracy levels the speed returned is generally ridiculously high. We're talking up to 200 mp/h (yes I know it's logged in metres/sec) when the phone is stationary.
I'm testing the same code base on two different model Android phones, running two different OS versions, and seeing the same issues so I expect this is a code issue.
What am I missing? I've tried averaging locations over a window of time, to no avail. Am I going to have to work out my own speed values based on distance travelled / time? This would be disappointing.
As you will see, I'm not doing anything special - a little filtering for accuracy, even after this both AverageSpeed
and _bestLocation.getSpeed()
are regularly unfeasibly high, even when accuracy of the location is good.
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;
}
}