googlemaps / android-maps-compose

Jetpack Compose composables for the Maps SDK for Android
https://developers.google.com/maps/documentation/android-sdk/maps-compose
Apache License 2.0
1.16k stars 142 forks source link

Polyline completely black when defining spans (v6.1.1) #613

Closed ninovanhooff closed 2 months ago

ninovanhooff commented 3 months ago

Android: 10, 14, physical + emulator. Probably all library version: 6.1.1 play-services-maps: 18.2.0 andoid-maps-utils 3.8.0

Steps to reproduce

  1. Use the sample in the Readme to create a polyline with a single gradient covering all points / steps

Code example

val uiSettings by remember {
    mutableStateOf(
      MapUiSettings(
        zoomControlsEnabled = false,
        zoomGesturesEnabled = true,
        compassEnabled = false,
      )
    )
  }
  val properties by remember {
    mutableStateOf(
      MapProperties(
        mapType = MapType.NONE,
        latLngBoundsForCameraTarget = mapBounds,
        minZoomPreference = 12f,
      )
    )
  }

  Box(modifier = modifier) {
    GoogleMap(
      modifier = Modifier
        .fillMaxSize(),
      uiSettings = uiSettings,
      properties = properties,
      cameraPositionState = cameraPositionState,
      onMapLoaded = {
        isMapLoaded = true
      }
    ) {
val points = currentFloorTotalRoute ?: return
  val styleSpan = StyleSpan(
    StrokeStyle.gradientBuilder(
      android.graphics.Color.CYAN,
      android.graphics.Color.RED,
    ).build(), Double.MAX_VALUE
  )

    val rememberedPoints = remember {
      points
    }

    val styleSpanList = remember {
      listOf(styleSpan)
    }

    Polyline(
      points = rememberedPoints,
      spans = styleSpanList,
      width = 10.dp.px(),
      zIndex = TOTAL_ROUTE_Z_INDEX
    )

Also tried defining colors like this:

Color.Red.toArgb(),
Color.Green.toArgb(),

Also tried omitting the segments parameter, and using a value of points.size.toDouble() per readme, but same effect. When using color = Color.Red I do see the expected red color.

Also tried omitting the width and zIndex params.

In the source I noticed that black is the fallback color, and the color is applied after the spans. could that be the cause? https://github.com/googlemaps/android-maps-compose/blob/53830c5e406a66a52f29d928a4ed1d414febb3e1/maps-compose/src/main/java/com/google/maps/android/compose/Polyline.kt#L185 https://github.com/googlemaps/android-maps-compose/blob/53830c5e406a66a52f29d928a4ed1d414febb3e1/maps-compose/src/main/java/com/google/maps/android/compose/Polyline.kt#L166

kikoso commented 2 months ago

Hi @ninovanhooff ,

I am trying to adapt your code into the BasicMapActivity, and the gradient works on the polyline. The fact that the default value (black) is taken is a good candidate for a race condition where the polyline color can't be somehow determined and the fallback is chosen instead.

Could you provide a repository with a sample I can use to check it out?

ninovanhooff commented 2 months ago

Thanks.

I transplanted as much of BasicMapActivity into my own app as possible and still seeing the black line image

I am migrating from xml to compose and have both the old and new dependencies in my project in different modules. My assumption now is that I have a dependency conflict where the old maps sdk is picked, which might not support span styles.

I am thus hoping/expecting that my problem will be resolved when removing the old Views-based maps dependency

ninovanhooff commented 2 months ago

Indeed!

Following these resources:

I found this line of code in my app:

MapsInitializer.initialize(applicationContext, MapsInitializer.Renderer.LEGACY, null)

Replacing that by

MapsInitializer.initialize(applicationContext)

Fixed the issue for me

image

kikoso commented 2 months ago

Nice to hear. The original code was forcing the legacy renderer, and that would not work with the spanned polyline.