java 使用 GPS 捕捉速度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11624947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the GPS to capture speed
提问by user964627
Possible Duplicate:
Detect a speed in android
可能的重复:
检测 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?
我正在进入 Android 开发,我想开始处理 GPS 和捕获速度。有没有办法可以使用 GPS 轻松捕获速度并在屏幕上显示该速度?我可以使用任何简单的框架或内置函数吗?
回答by Hesham Saeed
You will have to use LocationManagerwhich 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.
您将不得不使用LocationManager,它用于通过 GPS 获取用户位置,返回的 GPS 数据包括速度。您将不得不使用LocationListener的onLocationChanged(Location location)
功能。
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:
以 m/s 为单位,如果您需要将其转换为 km/h,请使用以下命令:
int speed=(int) ((location.getSpeed()*3600)/1000);
if you need to convert it to mph use this:
如果您需要将其转换为 mph,请使用以下命令:
int speed=(int) (location.getSpeed()*2.2369);
回答by Jomoos
I thinks there are two ways to achieve this:
我认为有两种方法可以实现这一目标:
Firstly, if you refer the Locationclass in android, you can find out that it has got a method named getSpeed
. It says:
首先,如果你在android中引用Location类,你会发现它有一个名为 getSpeed
. 它说:
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
其次,您可以跟踪 GPS 位置更新,以便存储该更新发生的先前位置和时间。现在,每次有新的更新时,您都可以使用类的distanceTo
方法找到行进的距离Location
。所以,要知道当前的速度,你可以使用公式,distance travelled / time difference between the updates