location.getspeed() update
Solution 1
Actually, GPS wont give exact information all the time. Even if you stay at same place, some times, latitude and longitude vary a little bit. I think it depends on minTime
and minDistance
you provide to requestLocationUpdates(...)
method.
Anyway, once try to change these values like below.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000L(minTime), 2000.0f(minDistance), myLocationListener);
Hope it helps.
Edit:
One more thing you can do is, according to the documentation, location.hasSpeed()
returns true
if this fix contains speed information, false
otherwise. When you stay at same place, it returns false
I guess.
Based on this, you can do something like setting value for textView
etc.
I have not tried but you can try once.
Solution 2
There are continuous, varying errors in GPS location fixes, due to things like atmosphere, satellite geometry, nearby buildings, etc.
The best you can do if you're trying to determine whether the GPS receiver is stationary or not is to check if the speed is below some credible threshold (e.g. 1kph).
You can do things like digitally filtering the speed value e.g.below is a simple adjustable low-pass filter that smooths out changes in the speed... put in a low value (e.g. 3) and the output will be quite responsive to changes in the input, put in a high value (e.g. 10) and the output becomes heavily smoothed.
/**
* Simple recursive filter
*
* @param prev Previous value of filter
* @param curr New input value into filter
* @return New filtered value
*
*/
private float filter(final float prev, final float curr, final int ratio) {
// If first time through, initialise digital filter with current values
if (Float.isNaN(prev))
return curr;
// If current value is invalid, return previous filtered value
if (Float.isNaN(curr))
return prev;
// Calculate new filtered value
return (float) (curr / ratio + prev * (1.0 - 1.0 / ratio));
}
filtSpeed = filter(filtSpeed, speed, 3);
Krystian Bersztolc
Updated on June 04, 2022Comments
-
Krystian Bersztolc almost 2 years
I have a litte problem. I located a
location.getSpeed()
method on theonLocationChanged()
method, and.getSpeed()
usually doesn't update/change to 0km/h when I stay in same place.Where should I locate a
location.getGpeed()
method, that the update showed 0km/h when I stay in place?And another question. This is my
locationListener
:private final LocationListener locationListener = new LocationListener() {public void onLocationChanged(Location location) { boolean var = false; float distance; if (firstRun) { distance=0; firstRun=false; } else distance = location.distanceTo (startLocation); Tspeed.setText("0km/h"); if (location.getSpeed()!=0) { Tspeed.setText(""+(location.getSpeed()*3600/1000 +"km/h")); } else { Tspeed.setText("0km/h"); } sAllDistance += distance; // startLocation=location; Tdistance.setText(""+(int)SAllDistance+" m"); }
The problem is: when the
LocationManager
get the fix, the firstAllDistance
returned fromlocation.distanceTo()
is ±50 meters.How can I fix it?