使用Android GPS定位纬度/经度精确计算车速(MPH)

使用Android GPS定位纬度/经度精确计算车速(MPH),android,android-gps,Android,Android Gps,我目前正在尝试开发一个Android应用程序,该应用程序使用GPS的位置纬度/经度,以英里/小时(MPH)为单位显示车辆的准确行驶速度 我第一次尝试使用以下代码:- private fun startLocationUpdates() { val locationRequest = LocationRequest.create()?.apply { interval = 1000 fastestInterval = 500 priority

我目前正在尝试开发一个Android应用程序,该应用程序使用GPS的位置纬度/经度,以英里/小时(MPH)为单位显示车辆的准确行驶速度

我第一次尝试使用以下代码:-

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
            }
        }
我的哈弗森计算(Km)如下:-

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
}
至英里:-

fun distanceMiles(departureLatitude: Double, departureLongitude: Double, destinationLatitude: Double, destinationLongitude: Double): Double {
    val distanceInKm = distance(departureLatitude, departureLongitude, destinationLatitude, destinationLongitude)
    return distanceInKm * 0.621371
}
由于我在计算中只使用了两点,所以我希望得到“紧张”的结果,但我看到的数字非常奇怪

我正在考虑使用Kotlin Flow将我的位置更新发送到平均函数,以平均出位置并获得更准确的速度值

我可以采取什么方法来获得更精确的速度


我的计算有错吗?

我想问题就在这里

 val currentTime = System.nanoTime()
 val time_s: Double = (currentTime - previousTimestamp) / (1_000_000_000.0 * 60.0 * 60.0)
使用
System.nanoTime()
,它以纳秒为单位返回当前时间

1秒=10^9纳秒

因此,正确的解决方案是:

val time_s: Double = (currentTime - previousTimestamp) / 1_000_000_000.0
我可以帮你。