Using the GPS to capture speed [duplicate]
Asked Answered
N

2

6

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?

Nee answered 24/7, 2012 at 5:56 Comment(1)
Thank You, I did a search earlier and nothing came up.Nee
N
30

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);
Nucleolated answered 24/7, 2012 at 6:27 Comment(0)
L
2

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

Lagting answered 24/7, 2012 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.