Tho*_*ena 5 android google-maps android-jetpack-compose composable
我尝试在我的项目中使用可组合项GoogleMap,但现在我需要LatLngBounds可见的谷歌地图onMapLoaded,以便我能够在这个边界内加载一些 POI,但我找不到如何获取此信息的答案。
有人可以给我线索吗?
如果您使用的是Google Maps Compose Library,则CameraPositionState包含 ,Projection其中包含 ,VisibleRegion
其中包含LatLngBounds。
如果将函数传递onMapLoaded给可组合项GoogleMap(与 一起cameraPositionState),则可以检查该值。
您还可以设置一个LaunchedEffect在相机移动时触发的事件,以在发生变化时报告边界。
我在编写本文时使用了以下依赖项
请参阅Maps Compose Repo 安装说明了解最新要求。
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Maps1Theme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
// a simple UI that shows the LatLngBounds at the top
// and a map below it
var text by remember { mutableStateOf("") }
Column {
Text(
text = text,
modifier = Modifier.padding(8.dp)
)
Map(
modifier = Modifier
.weight(1f)
.fillMaxWidth()
) {
text = it.toString()
}
}
}
}
}
}
}
@Composable
fun Map(
modifier: Modifier,
onBoundsChange: (LatLngBounds?) -> Unit,
) {
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(
LatLng(37.42423291057923, -122.08811454627153),
16f
)
}
// whenever cameraPositionState.isMoving changes, launch a coroutine
// to fire onBoundsChange. We'll report the visibleRegion
// LatLngBounds
LaunchedEffect(cameraPositionState.isMoving) {
if (!cameraPositionState.isMoving) {
onBoundsChange(
cameraPositionState.projection?.visibleRegion?.latLngBounds
)
}
}
GoogleMap(
modifier = modifier,
cameraPositionState = cameraPositionState,
// pass in an onMapLoaded to report the initial visible region
onMapLoaded = {
onBoundsChange(
cameraPositionState.projection?.visibleRegion?.latLngBounds
)
}
)
}
Run Code Online (Sandbox Code Playgroud)