Monday 30 November 2020

Accurately calculate Vehicle Speed (MPH) using Android GPS Location Latitude/Longitude

Im currently attempting to develop an Android Application that displays an accurate road speed for a vehicle in Mile Per Hour (MPH) using Location Latitude/Longitude from GPS.

I have the following code as my first attempt:-

private fun startLocationUpdates() {
    val locationRequest = LocationRequest.create()?.apply {
        interval = 1000
        fastestInterval = 500
        priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    }

    fusedLocationClient.requestLocationUpdates(locationRequest, manufactureCallBack(), Looper.getMainLooper())
}

       @SuppressLint("SetTextI18n")
        override fun onLocationResult(locationResult: LocationResult) {
            locationResult.locations.forEach { location ->
                previousTimestamp = if (this@MainActivity::previousLocation.isInitialized) {
                    val dist: Double = HaversineAlgorithm.distanceMiles(previousLocation.latitude, previousLocation.longitude, location.latitude, location.longitude)

                    val currentTime = System.nanoTime()
                    val time_s: Double = (currentTime - previousTimestamp) / (1_000_000_000.0 * 60.0 * 60.0)

                    Timber.i("time_s = $time_s :: dist = $dist")

                    if (dist > 0.0 && time_s > 0.0) {
                        val speed_mps: Double = dist / time_s
                        Timber.i("${location.time} speed_mps = $speed_mps")
                        val speed_mph: Double = speed_mps * something
                        milesPerHourTV.text = "$speed_mph MPH"
                    }
                    currentTime
                } else {
                    System.nanoTime()
                }

                previousLocation = location
            }
        }

My Haversine calculation (Km) is as follows:-

fun distance(departureLatitude: Double, departureLongitude: Double, destinationLatitude: Double, destinationLongitude: Double): Double {
    val latitudeDelta = Math.toRadians(destinationLatitude - departureLatitude)
    val longitudeDelta = Math.toRadians(destinationLongitude - departureLongitude)
    val radiusDepartureLatitude = Math.toRadians(departureLatitude)
    val radiusDestinationLatitude = Math.toRadians(destinationLatitude)
    val a = sin(latitudeDelta / 2).pow(2.0) + sin(longitudeDelta / 2).pow(2.0) * cos(radiusDepartureLatitude) * cos(radiusDestinationLatitude)
    val c = 2 * asin(sqrt(a))
    return EARTH_RADIUS * c
}

to Miles :-

fun distanceMiles(departureLatitude: Double, departureLongitude: Double, destinationLatitude: Double, destinationLongitude: Double): Double {
    val distanceInKm = distance(departureLatitude, departureLongitude, destinationLatitude, destinationLongitude)
    return distanceInKm * 0.621371
}

As I only use two points in my calculations I expected "jittery" results however the figures I see are very odd.

I was thinking of employing Kotlin Flow to send my Location updates to an Averaging function to average out the locations and obtain a more accurate velocity value.

What approach could I take to obtain a more accurate velocity?

Have I made a mistake in my calculations?



from Accurately calculate Vehicle Speed (MPH) using Android GPS Location Latitude/Longitude

No comments:

Post a Comment