I'm trying to use AndroidView to create a MapView with custom map tile overlay:
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = {
mapView
},
update = {mapView->
mapView.getMapAsync {googleMap ->
googleMap.mapType = GoogleMap.MAP_TYPE_NONE
googleMap.addTileOverlay(TileOverlayOptions().tileProvider(object :
UrlTileProvider(256, 256) {
private val baseUrl = resources.getString(R.string.custom_tile_url)
override fun getTileUrl(x: Int, y: Int, zoom: Int): URL? {
return URL(
baseUrl.replace("{z}", zoom.toString())
.replace("{x}", x.toString())
.replace("{y}", y.toString())
)
}
}))
}
}
)
mapView is initialized with rememberMapViewWithLifecycle referenced from another article.
However, I see the blank mapView : seems that GoogleMap.MAP_TYPE_NONE is in effect.
- If I comment out everything inside
getMapAsyncthen I get the default Google Map, so I'm pretty sure that I've got all the manifest and map initialization correct. - I have traced my code an sure that the formatted URL links to valid map tile. And the
getTileUrlfunction did get called.
I'm wondering if there's something I've missed?
Thanks.
OK, seems that I shouldn't put those in
updatestage. After moved those calls to the Lifecycle.Event.ON_CREATE, the map showing well.