mercredi 15 septembre 2021

MapGesture.OnGestureListener to let user select route they want

I'm trying to let the user decide which of the 3 routes provided, they want to take. Here is the screenshot of the routes. ScreenShot

I am using the HERE SDK FOR ANDROID (PREMIUM EDITION) 3.18.5

Here is my code for adding the routes to the map:

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()
                }
            }
        })

When I add them to the map, I make the first route the normal color, then the other two are in grey and they have a lower zIndex as to not cover up the selected route.

I am using the MapGesture.OnGestureListener to determine when one of the routes is selected. Here is the code.

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)

    }



}

This code keeps the originally selected route [0] highlighted and I thought it was supposed to just highlight the other route that was selected. I haven't worked making the originally selected route grey yet, or update the time and distance at the top of the screen, but when I use the code, it runs all of the Log calls in it.

I'm calling private var mapGestures: MapGesture.OnGestureListener? = null within the initMapFragmentView() where I have my other calls for 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 is the link to the HERE Docs for the Gestures.

I'm not positive if the issue is with the onMapObjectsSelected for loop, or how I have the if statements set up, or something else? When I click on any either of the grey polyLines, then all 3 of them get highlighted instead of just the one that was selected.

Aucun commentaire:

Enregistrer un commentaire