I’m in process of migrating Galway Bus over to use
Jetpack Compose and one particular requirement is the ability to
show bus stops (and also bus positions) on a Google Map. It seems like this is something that ultimately will exist
as specific Jetpack Compose @Composable
but for now it looks like only way to add a map like this is using AndroidView
.
Update: Since writing this article the Crane Sample was created. This builds out a more complete example of using Google Maps.
The following is what I currently have (very much open to suggestions for better way to do anything here)….it uses an
AndroidView
to show the map in a Column
above the list of nearby bus stops. As the user moves around on the map we call in to ViewModel
to set location
and also to trigger retrieval of updated list of bus stops (and in turn recomposition of UI). It also adds marker on map for each of the stops.
The map_layout.xml
layout file referred to in AndroidView
contains following
This is still very much work in progress and there are some known limitations around rendering of map if you “navigate” to another compose screen and back to this one again.
Featured in Kotlin Weekly Issue #208
Related tweets
Purpose of providing this is primarily to help “close the loop” and provide way to post feedback/ask questions etc.
Wrote a short post on approach I'm currently using to include Google Maps in a Jetpack Compose project
— John O'Reilly (@joreilly) July 25, 2020
(will update if/when I find different way of doing any of this....and, as always, any feedback warmly welcomed!) https://t.co/hkowOM86KM
For example, Leland Richardson has replied with suggestion to use emitView
(and to structure things somewhat differently)….
emitView
was introduced in Compose dev15 release which unfortunately we can’t use right now due to following
The dependency now on 1.4-M3 probably means we'll need to stay on Compose dev14 version for now when using in #KotlinMultiplatform project. Also waiting on 1.3.7-native-mt-1.4-M3 of Kotlinx Coroutines and looks like SQLDelight will not be updated until 1.4 release anyway. https://t.co/Ed5fGB0xLT
— John O'Reilly (@joreilly) July 22, 2020