MapGesture.OnGestureListener to let user select route they want

90 Views Asked by At

UPDATE BELOW!!

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.

UPDATE!!

I have made a few code changes trying to get this to work. In the process I have found out that the values are different for the same MapRoute. For example:

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

                    }

This will print out these values:

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

I'm now trying to find out why the values are different for the same MapRoute(routeResults[0].route) ??

Am I misunderstanding how the MapRoute is named? Because when I go to click on a Route on the map, it comes back with a different value as well. Is there a better way to determine which route a user has selected?

1

There are 1 best solutions below

0
On

The mapRoute = MapRoute(routeResults[0].route) statement in the above snippet is equivalent to MapRoute m_mapRoute = new MapRoute(route). As you have called the same method thrice, so it has created three different object.

For more details about the MapRoute class, please refer the below API reference document. https://developer.here.com/documentation/android-premium/3.18/api_reference_java/index.html?com%2Fhere%2Fandroid%2Fmpa%2Fmapping%2FMapRoute.html

Please refer the standard routing and map-gestures implementation examples from the below HERE github repository.

https://github.com/heremaps/here-android-sdk-examples