高速获取用户位置
Get user location at high speed
我一直在使用 MapBox 绘制地图和用户在地图上的位置。在使用位置引擎的同时,我订阅了位置更改。位置更改的问题是它们每秒发生一次(最多),而且我的用户大多开车,所以记录的位置经常落后。
我意识到即使记录的位置落后,显示用户当前位置的标记似乎总是准确的。我想知道是否有可能获得当前位置标记的 "estimated" 位置。
另外根据 mapbox 的文档,我可以使用 ProgressChangeListener
获得更准确的位置(也更频繁)。我试过这样设置,但没用。
val nav = MapboxNavigation(this,getString(R.string.access_token))
nav.addProgressChangeListener { location, routeProgress ->
Timber.tag("GPSDEBUG").d("GOT LOC UPDATE on ${System.currentTimeMillis()} with ${location.longitude},${location.latitude}")
}
nav.startNavigation(DirectionsRoute.fromJson(""))
显然 MapBox 不喜欢空路或虚假导航。
在我尝试其他建议(例如使用 Kalman 算法 "estimate" 缺失位置之前,如果能得到反馈,我会很高兴。
最终:主要目标是即使在高速下也能检索准确的 GPS 坐标。
我的解决方案是计算 pointA 和 pointB 之间的缺失位置,知道 GPS 提供更新的最大速率是 1s。在现实生活中,当人们在高速公路上高速行驶时,我不得不扣除 3 到 4 分。
private fun processMissingPoints() {
val stepInMeters = lastUsedLocation.distanceTo(lastKnownLocation) / (numberOfMissingPoints).toDouble()
val bearing = lastUsedLocation.bearingTo(lastKnownLocation)
missingPoints.forEach{ point ->
val newCoordinates = getNewCoordinates(lastUsedLocation.latitude , lastUsedLocation.longitude,stepInMeters*(index+1), bearing.toDouble())
}
}
/**
* http://www.movable-type.co.uk/scripts/latlong.html#dest-point
* Given a start point, initial bearing, and distance, this will calculate the destination
* point and final bearing travelling along a (shortest distance) great circle arc.
*/
private fun getNewCoordinates(latitude: Double,longitude: Double,distanceInMetres: Double,bearing: Double) : LatLng{
val brngRad = toRadians(bearing)
val latRad = toRadians(latitude)
val lonRad = toRadians(longitude)
val earthRadiusInMetres = 6371000
val distFrac = distanceInMetres / earthRadiusInMetres
val latitudeResult = asin(sin(latRad) * cos(distFrac) + cos(latRad) * sin(distFrac) * cos(brngRad))
val a = atan2(sin(brngRad) * sin(distFrac) * cos(latRad), cos(distFrac) - sin(latRad) * sin(latitudeResult))
val longitudeResult = (lonRad + a + 3 * PI) % (2 * PI) - PI
return LatLng(toDegrees(latitudeResult),toDegrees(longitudeResult))
}
我一直在使用 MapBox 绘制地图和用户在地图上的位置。在使用位置引擎的同时,我订阅了位置更改。位置更改的问题是它们每秒发生一次(最多),而且我的用户大多开车,所以记录的位置经常落后。
我意识到即使记录的位置落后,显示用户当前位置的标记似乎总是准确的。我想知道是否有可能获得当前位置标记的 "estimated" 位置。
另外根据 mapbox 的文档,我可以使用 ProgressChangeListener
获得更准确的位置(也更频繁)。我试过这样设置,但没用。
val nav = MapboxNavigation(this,getString(R.string.access_token))
nav.addProgressChangeListener { location, routeProgress ->
Timber.tag("GPSDEBUG").d("GOT LOC UPDATE on ${System.currentTimeMillis()} with ${location.longitude},${location.latitude}")
}
nav.startNavigation(DirectionsRoute.fromJson(""))
显然 MapBox 不喜欢空路或虚假导航。
在我尝试其他建议(例如使用 Kalman 算法 "estimate" 缺失位置之前,如果能得到反馈,我会很高兴。
最终:主要目标是即使在高速下也能检索准确的 GPS 坐标。
我的解决方案是计算 pointA 和 pointB 之间的缺失位置,知道 GPS 提供更新的最大速率是 1s。在现实生活中,当人们在高速公路上高速行驶时,我不得不扣除 3 到 4 分。
private fun processMissingPoints() {
val stepInMeters = lastUsedLocation.distanceTo(lastKnownLocation) / (numberOfMissingPoints).toDouble()
val bearing = lastUsedLocation.bearingTo(lastKnownLocation)
missingPoints.forEach{ point ->
val newCoordinates = getNewCoordinates(lastUsedLocation.latitude , lastUsedLocation.longitude,stepInMeters*(index+1), bearing.toDouble())
}
}
/**
* http://www.movable-type.co.uk/scripts/latlong.html#dest-point
* Given a start point, initial bearing, and distance, this will calculate the destination
* point and final bearing travelling along a (shortest distance) great circle arc.
*/
private fun getNewCoordinates(latitude: Double,longitude: Double,distanceInMetres: Double,bearing: Double) : LatLng{
val brngRad = toRadians(bearing)
val latRad = toRadians(latitude)
val lonRad = toRadians(longitude)
val earthRadiusInMetres = 6371000
val distFrac = distanceInMetres / earthRadiusInMetres
val latitudeResult = asin(sin(latRad) * cos(distFrac) + cos(latRad) * sin(distFrac) * cos(brngRad))
val a = atan2(sin(brngRad) * sin(distFrac) * cos(latRad), cos(distFrac) - sin(latRad) * sin(latitudeResult))
val longitudeResult = (lonRad + a + 3 * PI) % (2 * PI) - PI
return LatLng(toDegrees(latitudeResult),toDegrees(longitudeResult))
}