https://kotlinlang.org logo
Title
j

Jérémy CROS

08/16/2022, 10:08 AM
Hi everyone! 🙂 We’re trying to integrate a map (via MapBox) in our app and we’re having a hard time doing it “the proper compose way”. Especially when it comes to handling states and drawing/not re-drawing elements Simplified code in thread:
The base setup of the map composable looks like that:
AndroidView(
    modifier = Modifier
        .fillMaxWidth()
        .constrainAs(map) {
            top.linkTo(<http://parent.top|parent.top>)
            bottom.linkTo(<http://bottomBar.top|bottomBar.top>)
        },
    factory = { context ->
        MapView(
            context,
            mapInitOptions = MapInitOptions(
                context,
                mapResourceOptions,
                styleUri = mapStyleUri
            )
        )
    },
    update = {
        // Looking to do stuff here
    }
)
We have two goals: drawing a user roadmap “ideally the first time” to save computation And then drawing the user location whenever it’s updated. So far, our idea is to do everything in the update lambda our composable would have some parameters : the roadmap, the user location, and maybe a boolean shouldDrawRoadmap And the update would do :
if (shouldDrawRoadmap) { mapview.drawRoadmap() }
mapview.drawUserLocation()
But it seems strange... Like there’s a better way to do it with states or remembers or something else.... Any idea that could point us in the right direction? 🙂 Thanks for any help! 🙏
c

Colton Idle

08/17/2022, 3:40 PM
I'd take a look at google maps compose as I'm fairly certain they are just building on top of AndroidView https://github.com/googlemaps/android-maps-compose and check out this blog post on how they converted maps to compose. https://medium.com/androiddevelopers/diving-into-compose-lessons-learned-while-building-maps-compose-d20ef5dfe1bb
j

Jérémy CROS

08/17/2022, 5:06 PM
That was a very interesting read, thank you very much 🙏