I need to calculate distance traveled by car! Not distance between, not distance to no. The distance can be fully different if we calculate it via Google's provided API. Google can give 1KM from one point to another but car can go 800 meters in the way the rider want. Using Accelerometer didn't help. It works for walking but never for faster speed.
Any suggestions on how can I get the distance traveled by the car?
I've tried using Location APIs of Google: distanceTo or distanceBetween is not an option at all. It can give significant different result than IN REAL. In real car can travel through very short places and reach goal in 800 meters whereas Google can give 1KM distance between locations.
Below is the code for my application. The speed is amazingly correct.
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()
}
}