简体   繁体   English

如何从jetpack compose中的可见地图获取LatLngBounds

[英]How to get LatLngBounds from visible map in jetpack compose

I try to use the composable GoogleMap in my project but now I need the LatLngBounds of the visible google map in onMapLoaded so I'm able to load some POI for within this boundaries, but I can't find an answer on how I could get this information.我尝试在我的项目中使用可组合的谷歌地图,但现在我需要GoogleMap onMapLoaded LatLngBounds我能够在此边界内加载一些 POI,但我找不到关于如何获得的答案此信息。

May anybody have a clue for me?有人可以告诉我吗?

If you're using Google Maps Compose Library , the CameraPositionState contains a Projection which contains a VisibleRegion which contains a LatLngBounds .如果您使用的是Google Maps Compose Library ,则CameraPositionState包含一个Projection ,该 Projection 包含一个VisibleRegion ,其中包含一个LatLngBounds

If you pass in an onMapLoaded function to the GoogleMap composable (along with a cameraPositionState ), you can check this value.如果您将onMapLoaded函数传递给可组合的GoogleMap (连同cameraPositionState ),则可以检查此值。

You can also set up a LaunchedEffect that triggers whenever the camera moves to report the bounds whenever it changes.您还可以设置一个LaunchedEffect ,它会在相机移动时触发,以便在它发生变化时报告边界。

I used the following dependencies at the time I'm writing this我在写这篇文章时使用了以下依赖项

  • Jetpack Compose: 1.2.0-beta02 Jetpack 组合:1.2.0-beta02
  • Kotlin: 1.6.21科特林:1.6.21
  • Maps Compose: 2.2.1地图组成:2.2.1
  • Play Services Maps:18.0.2播放服务地图:18.0.2

See theMaps Compose Repo Install Instructions for up-to-date requirements.有关最新要求,请参阅Maps Compose 存储库安装说明

// 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
            )
        }
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM