Possible Duplicate:
Detect a speed in android
I am getting into Android development and I would like to start messing around with the GPS and capturing speeds. Is there a way I can easily capture the speed using the gps and display that speed on the screen? Any simple frameworks or built in functions I could use?
You will have to use LocationManager which is used to get user location through GPS, the GPS data that returns includes the speed. You will have to use the onLocationChanged(Location location)
function of the LocationListener.
To get the speed you will need to do this:
int speed = location.getSpeed();
which is in m/s, if you need to convert it to km/h use this:
int speed=(int) ((location.getSpeed()*3600)/1000);
if you need to convert it to mph use this:
int speed=(int) (location.getSpeed()*2.2369);
I thinks there are two ways to achieve this:
Firstly, if you refer the Location class in android, you can find out that it has got a method named getSpeed
. It says:
Returns the speed of the device over ground in meters/second.
If hasSpeed() is false, 0.0f is returned.
So, you may write, something like this
if (locationObj.hasSpeed()) {
float speed = locationObj.getSpeed();
// process data
} else {
// Speed information not available.
}
Secondly, you may track gps location updates, so that, you store previous location and time at which that update occurs. Now, each time a new update comes you can find the distance travelled using distanceTo
method of Location
class. So, to know the current speed, you may use the formula, distance travelled / time difference between the updates
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With