📅  最后修改于: 2023-12-03 15:13:20.186000             🧑  作者: Mango
Android Geolocation provides location data to the applications in your android device, which includes the speed of the device. This speed value can be given in either meters per second(m/s) or kilometers per hour(km/h).
The speed unit provided by the Android Geolocation depends on the getSpeed()
method used to get the speed value.
getSpeed()
To get the speed value from the Android Geolocation, you can use the getSpeed()
method which returns the device speed in meters per second.
...
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
float speed = location.getSpeed();
Log.d(TAG, "Device speed in meter per seconds: " + speed);
}
...
This will return a float value that represents the speed of the device in meters per second.
km/h
To convert the speed unit from meters per second to kilometers per hour, you can use the following code:
...
float speed = location.getSpeed();
float speedInKmH = (speed * 3600) / 1000;
Log.d(TAG, "Device speed in kilometer per hour: " + speedInKmH);
...
This will give you the speed of the device in kilometers per hour.
In this guide, we learned how to get the speed of the device using the Android Geolocation. The speed unit provided by the getSpeed()
method is in meters per second. We also learned how to convert this unit to kilometers per hour using simple multiplication and division operations in Java.