下面更新!!
我试图让用户决定他们想要采取的 3 条路线中的哪一条。这是路线的屏幕截图。
我正在使用 HERE SDK FOR ANDROID (PREMIUM EDITION) 3.18.5
这是我将路线添加到地图的代码:
coreRouter.calculateRoute(routePlan,
object : Router.Listener<List<RouteResult>, RoutingError> {
override fun onProgress(i: Int) {
}
override fun onCalculateRouteFinished(
routeResults: List<RouteResult>,
routingError: RoutingError
) {
if (routingError === RoutingError.NONE) {
routeResultsList = routeResults
if (routeResults[0].route != null) {
route = routeResults[0].route
mapRoute = MapRoute(routeResults[0].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.zIndex = 3
mapRoute!!.tag = "0"
map!!.addMapObject(mapRoute!!)
geoBoundingBox = routeResults[0].route.boundingBox
geoBoundingBox!!
map!!.zoomTo(
geoBoundingBox!!, Map.Animation.NONE, 5f
)
timeDistanceForCL()
if (onPause == 1) {
startNavigation()
}
} else {
Toast.makeText(
this@Route,
"Error: route results returned is not valid",
Toast.LENGTH_LONG
).show()
}
if (routeResults[1].route != null) {
route = routeResults[1].route
mapRoute = MapRoute(routeResults[1].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.color = ContextCompat.getColor(this@Route, R.color.gray_lines)
mapRoute!!.zIndex = 2
mapRoute!!.tag = "1"
map!!.addMapObject(mapRoute!!)
if (onPause == 1) {
startNavigation()
}
} else {
Toast.makeText(
this@Route,
"Error: route results returned is not valid",
Toast.LENGTH_LONG
).show()
}
if (routeResults[2].route != null) {
route = routeResults[2].route
mapRoute = MapRoute(routeResults[2].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.color = ContextCompat.getColor(this@Route, R.color.gray_lines)
mapRoute!!.zIndex = 1
mapRoute!!.tag = "2"
map!!.addMapObject(mapRoute!!)
if (onPause == 1) {
startNavigation()
}
} else {
Toast.makeText(
this@Route,
"Error: route results returned is not valid",
Toast.LENGTH_LONG
).show()
}
} else {
Toast.makeText(
this@Route,
"Error: route calculation returned error code: $routingError",
Toast.LENGTH_LONG
).show()
}
}
})
当我将它们添加到地图时,我将第一条路线设为正常颜色,然后其他两条路线为灰色,并且它们的 zIndex 较低,以免覆盖所选路线。
我正在使用 MapGesture.OnGestureListener 来确定何时选择了其中一条路线。这是代码。
private val mapG: MapGesture.OnGestureListener = object : MapGesture.OnGestureListener.OnGestureListenerAdapter() {
override fun onMapObjectsSelected(p0: MutableList<ViewObject>): Boolean {
Log.d("onMapObjectsSelected", "onMapObjectsSelected ran")
for (p0 in routeResultsList) {
Log.d("onMapObjectsSelected", "onMapObjectsSelected for loop ran ran")
if (p0.route == routeResultsList[0].route){
Log.d("onMapObjectsSelected", "onMapObjectsSelected if(1) ran ran")
route = routeResultsList[0].route
mapRoute = MapRoute(routeResultsList[0].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.zIndex = 3
map!!.addMapObject(mapRoute!!)
}
if (p0.route == routeResultsList[1].route){
Log.d("onMapObjectsSelected", "onMapObjectsSelected if(2) ran ran")
route = routeResultsList[1].route
mapRoute = MapRoute(routeResultsList[1].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.zIndex = 3
map!!.addMapObject(mapRoute!!)
}
if (p0.route == routeResultsList[2].route){
Log.d("onMapObjectsSelected", "onMapObjectsSelected if(3) ran ran")
route = routeResultsList[2].route
mapRoute = MapRoute(routeResultsList[2].route)
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.zIndex = 3
map!!.addMapObject(mapRoute!!)
}
}
return super.onMapObjectsSelected(p0)
}
}
此代码使最初选择的路线 [0] 突出显示,我认为它应该只突出显示选择的另一条路线。我还没有将最初选择的路线设为灰色,或者在屏幕顶部更新时间和距离,但是当我使用代码时,它会运行其中的所有 Log 调用。
我正在调用私有 var mapGestures: MapGesture.OnGestureListener?= null 在 initMapFragmentView() 中我还有其他对 NavigationListener 的调用。
private fun initMapFragmentView() {
val path = File(getExternalFilesDir(null), ".here-map-data")
.absolutePath
MapSettings.setDiskCacheRootPath(path)
val mapFragment = supportFragmentManager.findFragmentById(R.id.mapFragment) as AndroidXMapFragment?
val context = ApplicationContext(this).apply {
setAppIdCode(s, s1)
setLicenseKey(s2)
}
mapFragment?.let { fragment ->
fragment.init(context) { error ->
when (error) {
OnEngineInitListener.Error.NONE -> {
map = fragment.map
map?.run {
setCenter(
GeoCoordinate(36.9566664, -94.7881218, 0.0),
Map.Animation.NONE
)
setZoomLevel((maxZoomLevel + minZoomLevel) / 2)
}
navigationManager = NavigationManager.getInstance()
navigationManager!!.distanceUnit = NavigationManager.UnitSystem.IMPERIAL_US
navigationManager!!.addRerouteListener(
WeakReference(
mNavigaionRerouteListener
)
)
fragment.mapGesture?.addOnGestureListener(mapG, 0, true)
navigationManager!!.realisticViewMode = NavigationManager.RealisticViewMode.DAY
navigationManager!!.addRealisticViewAspectRatio(NavigationManager.AspectRatio.AR_4x3)
navigationManager!!.addRealisticViewListener(
WeakReference(viewListener))
voiceNavigation()
initNavigation()
}
else -> {
val errorMessage = "Error: ${error}, SDK Version: ${Version.getSdkVersion()}"
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
}
}
}
这是手势的 HERE Docs的链接。
如果问题出在 onMapObjectsSelected for 循环上,或者我如何设置 if 语句或其他问题,我不肯定?当我单击任何一条灰色折线时,它们中的所有 3 条都会突出显示,而不仅仅是选中的那条。
更新!!
我做了一些代码更改,试图让它工作。在此过程中,我发现同一 MapRoute 的值不同。例如:
if (routeResults[0].route != null) {
route = routeResults[0].route
mapRoute = MapRoute(routeResults[0].route)
mapRoute0 = MapRoute(routeResults[0].route)
val toAdd = MapRoute(routeResults[0].route)
Log.d("onMapObjectsSelected", "mapRouteList0 $toAdd")
Log.d("onMapObjectsSelected", "mapRoute $mapRoute")
Log.d("onMapObjectsSelected", "mapRoute0 $mapRoute0")
mapRoute!!.isManeuverNumberVisible = true
mapRoute!!.zIndex = 3
mapRoute!!.tag = "0"
mapRouteList.add(toAdd)
map!!.addMapObject(mapRoute!!)
geoBoundingBox = routeResults[0].route.boundingBox
geoBoundingBox!!
map!!.zoomTo(
geoBoundingBox!!, Map.Animation.NONE, 5f
)
timeDistanceForCL()
if (onPause == 1) {
startNavigation()
}
}
这将打印出这些值:
2021-09-18 12:34:35.356 24400-24400/com.reedscodingandpublishingllc.truckparkingmore D/onMapObjectsSelected: mapRouteList0 com.here.android.mpa.mapping.MapRoute@c5f698ff
2021-09-18 12:34:35.356 24400-24400/com.reedscodingandpublishingllc.truckparkingmore D/onMapObjectsSelected: mapRoute com.here.android.mpa.mapping.MapRoute@c5f68e0f
2021-09-18 12:34:35.356 24400-24400/com.reedscodingandpublishingllc.truckparkingmore D/onMapObjectsSelected: mapRoute0 com.here.android.mpa.mapping.MapRoute@c5f6988f
我现在试图找出为什么相同 MapRoute(routeResults[0].route) 的值不同?
我是否误解了 MapRoute 的命名方式?因为当我点击地图上的路线时,它也会返回不同的值。有没有更好的方法来确定用户选择了哪条路线?