Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the GPS to capture speed [duplicate]

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?

like image 483
user964627 Avatar asked Nov 28 '22 07:11

user964627


2 Answers

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);
like image 119
Hesham Saeed Avatar answered Dec 04 '22 21:12

Hesham Saeed


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

like image 24
Jomoos Avatar answered Dec 04 '22 20:12

Jomoos