[英]Mapbox map is black using jetpack compose
I'm trying to use Mapbox with compose, but the map is not showing correctly.我正在尝试将 Mapbox 与 compose 一起使用,但 map 显示不正确。 I try:
我尝试:
@Composable
fun MapView(
modifier: Modifier = Modifier
) {
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context ->
var map = MapView(context).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
getMapboxMap().apply {
cameraOptions {
zoom(19.0)
}
}
}
map
}
)
}
But display empty:但显示为空:
You firstly have to get the instance of the mapbox with您首先必须获得地图框的实例
Mapbox.getInstance(context, public_api_key)
Then inside the mapview you have to get the map asynchronously.然后在地图视图中,您必须异步获取 map。
MapView(context).apply {
getMapAsync { mapboxMap ->
mapboxMap.setStyle(Style.MAPBOX_STREETS)
val position = CameraPosition.Builder()
.zoom(19.0)
.build()
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), 1)
}
}
For the full snippet对于完整的片段
AndroidView(
modifier = modifier,
factory = { context ->
Mapbox.getInstance(
context,
public_api_key
)
MapView(context).apply {
getMapAsync { mapboxMap ->
mapboxMap.setStyle(Style.MAPBOX_STREETS)
val position = CameraPosition.Builder()
.zoom(19.0)
.build()
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), 1)
}
}
}
)
You can set public_api_key as mapbox_access_token
in string resource file您可以在字符串资源文件中将 public_api_key 设置为
mapbox_access_token
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.