使用安卓GPS定位经纬度准确计算车速(英里/小时)

3

我目前正在尝试开发一个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
            }
        }

我使用的Haversine公式计算距离(单位为千米)如下:

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
}

我的计算只使用了两个点,因此我预期会得到“抖动”的结果,然而我看到的数字非常奇怪。

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

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

我在我的计算中是否犯了错误?


https://dev59.com/rGUp5IYBdhLWcg3wCUCx 可以帮助你。 - Bek
1个回答

1
我认为问题在这里。
 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接