【发布时间】:2019-02-11 10:34:04
【问题描述】:
我需要计算汽车行驶的距离!没有距离,没有距离没有。如果我们通过 Google 提供的 API 计算距离,距离可能会完全不同。谷歌可以从一个点到另一个点 1 公里,但汽车可以按照骑手想要的方式行驶 800 米。使用加速度计没有帮助。它适用于步行,但不适用于更快的速度。
关于如何获得汽车行驶距离的任何建议?
我尝试过使用 Google 的 Location API:distanceTo 或 distanceBetween 根本不是一个选项。它可以给出与 IN REAL 截然不同的结果。在真正的汽车中,可以通过非常短的地方并在 800 米内到达目标,而 Google 可以提供 1KM 的位置之间的距离。
以下是我的应用程序的代码。速度惊人的正确。
class HomeScreen : AppCompatActivity(), GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, SensorEventListener {
override fun onSensorChanged(event: SensorEvent?) {
val sensor = event?.sensor
val values = event?.values
var value = -1
if (values != null && values.size ?: 0 > 0) {
value = values[0].toInt()
}
if (sensor != null &&
sensor.type == Sensor.TYPE_STEP_DETECTOR
) {
val finalSteps = getDistanceRun(steps)
val finalStepsTruncated = String.format("%.2f", finalSteps)
distanceTV.text = "$finalStepsTruncated"
steps++
}
}
override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
}
override fun onConnectionFailed(p0: ConnectionResult) {
val failed = p0
}
@SuppressLint("MissingPermission")
override fun onConnected(p0: Bundle?) {
if (locationPermissionsGranted(this)) {
fusedLocationClient?.requestLocationUpdates(locationRequest, object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?) {
val location = p0
val metersPerSecond: Float = location?.lastLocation?.speed ?: 0f
val speed = metersPerSecond * 3600 / 1000
speedTV.text = "${Math.round(speed)} KM/H"
}
}, null)
} else {
requestPermission(
this, 0,
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION
)
}
}
override fun onConnectionSuspended(p0: Int) {
val suspended = p0
}
private var fusedLocationClient: FusedLocationProviderClient? = null
private var mGoogleApiClient: GoogleApiClient? = null
private lateinit var locationRequest: LocationRequest
private var steps: Long = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_screen)
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
locationRequest.interval = 1000
locationRequest.fastestInterval = 500
if (PermissionManager.locationPermissionsGranted(this)) {
mGoogleApiClient = GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build()
mGoogleApiClient?.connect()
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
createLocationRequest()
} else {
requestPermission(
this, 0,
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION
)
}
val sManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val stepSensor = sManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR)
sManager.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (PermissionManager.locationPermissionsGranted(this)) {
mGoogleApiClient = GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build()
mGoogleApiClient?.connect()
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
createLocationRequest()
}
}
protected fun createLocationRequest() {
val builder = LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest)
val client = LocationServices.getSettingsClient(this)
val task = client.checkLocationSettings(builder.build())
task.addOnSuccessListener(this) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
}
task.addOnFailureListener(this) { e ->
if (e is ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
e.startResolutionForResult(
this@HomeScreen,
0
)
} catch (sendEx: IntentSender.SendIntentException) {
// Ignore the error.
}
}
}
}
fun getDistanceRun(steps: Long): Float {
return (steps * 78).toFloat() / 100000.toFloat()
}
}
【问题讨论】:
-
也许每秒或每 20 米获取 GPS 更新,并总结整个距离的所有位移(?)如果您每 20 米请求它们,那么您甚至不需要计算位移。
标签: java android kotlin distance