use*_*627 4 java gps android android-4.2-jelly-bean
可能重复:
检测android中的速度
我正在进入Android开发阶段,我想开始搞乱GPS并捕捉速度.有没有办法可以使用gps轻松捕捉速度并在屏幕上显示速度?我可以使用任何简单的框架或内置函数?
Hes*_*eed 28
您将不得不使用用于通过GPS获取用户位置的LocationManager,返回的GPS数据包括速度.您必须使用LocationListener的onLocationChanged(Location location)功能.
要获得速度,您需要这样做:
int speed = location.getSpeed();
Run Code Online (Sandbox Code Playgroud)
以m/s为单位,如果需要将其转换为km/h,请使用:
int speed=(int) ((location.getSpeed()*3600)/1000);
Run Code Online (Sandbox Code Playgroud)
如果你需要将它转换为mph使用这个:
int speed=(int) (location.getSpeed()*2.2369);
Run Code Online (Sandbox Code Playgroud)
我认为有两种方法可以实现这一目标:
首先,如果你引用android中的Location类,你可以发现它有一个名为 的方法 getSpeed。它说:
Returns the speed of the device over ground in meters/second.
If hasSpeed() is false, 0.0f is returned.
Run Code Online (Sandbox Code Playgroud)
所以,你可以写这样的东西
if (locationObj.hasSpeed()) {
float speed = locationObj.getSpeed();
// process data
} else {
// Speed information not available.
}
Run Code Online (Sandbox Code Playgroud)
其次,您可以跟踪 GPS 位置更新,以便存储以前的位置和更新发生的时间。distanceTo现在,每次有新的更新时,您都可以使用类方法找到行驶的距离Location。因此,要知道当前速度,您可以使用以下公式:distance travelled / time difference between the updates
| 归档时间: |
|
| 查看次数: |
14056 次 |
| 最近记录: |