Home > database >  how to launch a Google Maps Marker inside a Coroutine?
how to launch a Google Maps Marker inside a Coroutine?

Time:09-14

I am trying to figure out how to launch a google maps marker inside a coroutine, to make it so the markers load seperate from the main thread that will launch the google maps. I hope this will make the user experience more smooth when starting up google maps. How can I achieve this?

Obs! When I launch it like this I get an error from Marker: "@Composable invocations can only happen from the context of a @Composable function"

My code:

                scope.launch(Dispatchers.IO) {
                    locations.forEach {
                            places ->
                        val allLocations = LatLng(places.lat, places.lng)
                        Marker(
                            state = MarkerState(allLocations),
                            title = places.name,
                            snippet = places.snippet
                        )
                    }
                }

CodePudding user response:

There is no need to do it inside a coroutine scope. Because if you check the implementation of GoogleMap composable you will see, that it will do this operation inside a LaunchedEffect (that provides a coroutine scope).

GoogleMap.kt (version 2.1.0) line 116:

LaunchedEffect(Unit) {
    disposingComposition {
            ...

            currentContent?.invoke()
        }
    }
}

the currentContent is the markers that you have added to GoogleMap.

  • Related