Hi Everyone! I'm using <Mapbox with JetpackCompose...
# compose-android
f
Hi Everyone! I'm using Mapbox with JetpackCompose library and I'm able to render a GeoJSON, but whenever I update it the geoJSON (it is passed as parameter) the map its not updated. Does anybody knows how to update the rendered GeoJSON? 🗺️ I see that you can work with the layers themselves, and add/delete them at runtime but I haven't fully understood yet, so any help there would also be nice. (the code will be on a comment below if you're interested)
I'm passing a pair where I have an ID as first and the geoJSON as the second, (I wanted to try if by chaning the
key
would trigger the re-render, but nop...
Copy code
@Composable
actual fun MapView(
    geoJSON: Pair<String,String>?
) {
geoJSON?.let { (id, datas) ->
        println(">> Re render?")

recomposition when the data changes
        var geoJsonData by remember { mutableStateOf(datas) }
        val mapViewportState = remember { MapViewportState() }

        val mapState = rememberMapState(key = id)
        val geoJsonSource = rememberGeoJsonSourceState(key = id) {
            data = GeoJSONData(datas)
        }

recomposition
        Scaffold { padding ->
            MapboxMap(
                modifier = Modifier.fillMaxSize(),
                scaleBar = {},
                mapViewportState = mapViewportState,
                compass = {
                    Compass(Modifier.padding(top = padding.calculateTopPadding()))
                },
                style = {
                    MapboxStandardSatelliteStyle {
                        lightPreset = LightPresetValue.DAWN
                        showRoadsAndTransit = BooleanValue(false)
                        showPointOfInterestLabels = BooleanValue(false)
                        showPlaceLabels = BooleanValue(false)
                    }
                },
            ) {
                val sourceState = rememberGeoJsonSourceState(key = "geojson-source") {
                    data = GeoJSONData(geoJsonData)
                }


                LineLayer(sourceState = sourceState) {
                    lineColor = ColorValue(Color.Blue)
                    lineWidth = DoubleValue(2.0)
                }
            }
        }
}
}
p
Not sure what
rememberGeoJsonSourceState()
does internally but hardcoding the value of
key
seems to me could trigger this type of issues due to positional memoization. Try using geoJsonData as key and see what happens