Search code examples
android-auto

RoutePreviewNavigationTemplate Doesn;t show map and routes


Hi I'm trying to implement RoutePreviewNavigationTemplate but have some troubles:

1.- Routes List is displayed but the map in background is not showed

2.- For this reason i can't draw the route between my current location and place A or B

this is my code where I build the template, 2 routes and the list of them

class RoutePreviewScreen(carContext: CarContext) : Screen(carContext) {
@RequiresApi(Build.VERSION_CODES.O)
@SuppressLint("UnsafeOptInUsageError")
override fun onGetTemplate(): Template {
    val builder = RoutePreviewNavigationTemplate.Builder()



    val locationA = CarLocation.create(19.2689369, -103.7636865)
    val locationB = CarLocation.create(19.269160, -103.754996)

    val markerA = PlaceMarker.Builder().setColor(CarColor.RED).build()
    val markerB = PlaceMarker.Builder().setColor(CarColor.BLUE).build()

    val placeA = Place.Builder(locationA).setMarker(markerA).build()
    val placeB = Place.Builder(locationB).setMarker(markerB).build()

    val routeItem = Row.Builder()
        .setTitle(CarText.create("From current to A"))
        .addText(SpannableString(" ").apply {
            setSpan(
                DistanceSpan.create(
                    Distance.create(Math.random() * 100, Distance.UNIT_KILOMETERS)
                ), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE
            )
        })
        .addText(SpannableString(" ").apply {
            setSpan(
                DurationSpan.create(
                    Duration.ofHours(3)
                ), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE
            )
        })
        .setMetadata(
            Metadata.Builder()
                .setPlace(placeA)
                .build()
        )
        .build()

    val routeItem2 = Row.Builder()
        .setTitle(CarText.create("From current location to A"))
        .addText(SpannableString(" ").apply {
            setSpan(
                DistanceSpan.create(
                    Distance.create(Math.random() * 200, Distance.UNIT_KILOMETERS)
                ), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE
            )
        })
        .addText(SpannableString(" ").apply {
            setSpan(
                DurationSpan.create(
                    Duration.ofHours(3)
                ), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE
            )
        })
        .setMetadata(
            Metadata.Builder()
                .setPlace(placeB)
                .build()
        )
        .build()


    val itemList = ItemList.Builder()
        .addItem(routeItem)
        .addItem(routeItem2)
        .setOnSelectedListener {
            //TODO SELECTED
        }
        .build()

    builder.setItemList(itemList)

    val navigateAction = Action.Builder()
        .setTitle("Start Navigation")
        .setOnClickListener {
         //TODO IMPLEMENT NAVIGATION
        }
        .build()

    builder.setNavigateAction(navigateAction)

    return builder.build()
}

}


Solution

  • Routes List is displayed but the map in background is not showed

    For navigation apps (those that use the NavigationTemplate and supporting templates like the MapTemplate, RoutePreviewNavigationTemplate, and PlaceListNavigationTemplate), you are responsible for drawing the map yourself. See Draw the map for more details. https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:car/app/app-samples/navigation/ also has a sample app that you can reference, with SurfaceRenderer being of particular interest there.

    This is in contrast to point of interest (POI) apps, which use the PlaceListMapTemplate, which handles drawing the map and markers for you.