English 中文(简体)
如何自动调整动物群,以适应谷歌地图集的所有标识?
原标题:How to automatically adjust zoom to accommodate all markers in Google Maps Compose?

谷歌地图 相容图书馆提供<代码>cameraPositionState 至accomodate 1个地点,使用以下代码:

val singapore = LatLng(1.35, 103.87)
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(singapore, 10f)
}
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraPositionState)

如何完成同一个证人多个地点? 我有一份地点清单,我想首先显示所有地点标识。

问题回答

Use LatLngBounds.builder() to create a minimum bound based on a set of LatLng points, then use cameraPositionState.move() to update the camera to the specified bounds

您不使用<代码>LatLngs来说明照相机状况,而是可使用<代码>LatLngBounds代替。 a. 最新数据。

但你首先需要<代码>LatLngBounds。 在这种情况下,你可以使用<条码>LatLngBounds.builder()方法,在你的照相更新中使用<条码>的条目<

因此,我要说,我有用于瞬间标识的以下坐标:

private val santiago = LatLng(-33.4489, -70.6693)
private val bogota = LatLng(-4.7110, -74.0721)
private val lima = LatLng(-12.0464, -77.0428)
private val salvador = LatLng(-12.9777, -38.5016)

一俟我有此需要,我就不得不立即进行建设:

val boundsBuilder = LatLngBounds.builder()

Create a list for the coordinates:

val coordinates = listOf(
            santiago,
            bogota,
            lima,
            salvador
        )

然后通过该编码使用<代码>include(>方法>builder(<>/code>通过每一条 LatLng协调:

for (coordinate in coordinates) {
    boundsBuilder.include(coordinate)
}

然后用<代码>build()方法加以充实,并储存其作为<代码>限值<<<> >限值/代码>的返回价值,以供日后用于更新你的照相机。

val bounds = boundsBuilder.build()

然后,在您的<代码>setContent内,如果想更新最初的Load的照相机,使之包括所有标识,那么,你只能使用.camerasPosmove()。 可在<条码>范围内使用。 它想看这样的情况:

LaunchedEffect(key1 = true ){
    cameraPositionState.move(
        update = CameraUpdateFactory.newLatLngBounds(bounds, 100)
    )
}

通知cameraPositionState.move() 方法有update para amount。 您刚才不得不将<条码>输入数值乘以<条码>上<>。 升值为CameraUpdateFactory.newLatLngBounds (BOUNDS_VALUE_HERE, PADDING_VALUE_HERE)

这里是你可以尝试的样本,在我给你增加一个子子,在最初装载量之外测试照相机的最新情况:

private val santiago = LatLng(-33.4489, -70.6693)
private val bogota = LatLng(-4.7110, -74.0721)
private val lima = LatLng(-12.0464, -77.0428)
private val salvador = LatLng(-12.9777, -38.5016)
private val center = LatLng(-18.000, -58.000)
private val defaultCameraPosition1 = CameraPosition.fromLatLngZoom(center, 2f)

class StackOverflowSample : ComponentActivity(), OnMapsSdkInitializedCallback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MapsInitializer.initialize(applicationContext, MapsInitializer.Renderer.LATEST, this)
        val boundsBuilder = LatLngBounds.builder()

        val coordinates = listOf(
            santiago,
            bogota,
            lima,
            salvador
        )

        for (coordinate in coordinates) {
            boundsBuilder.include(coordinate)
        }
        val bounds = boundsBuilder.build()
        setContent {
            // Observing and controlling the camera s state can be done with a CameraPositionState
            val cameraPositionState = rememberCameraPositionState {
                position = defaultCameraPosition1
            }
            val mapProperties by remember {
                mutableStateOf(MapProperties(mapType = MapType.NORMAL))
            }
            val marker1State = rememberMarkerState(position = santiago)
            val marker2State = rememberMarkerState(position = bogota)
            val marker3State = rememberMarkerState(position = lima)
            val marker4State = rememberMarkerState(position = salvador)

            // Drawing on the map is accomplished with a child-based API
            val markerClick: (Marker) -> Boolean = {
                Log.d(TAG, "${it.title} was clicked")
                cameraPositionState.projection?.let { projection ->
                    Log.d(TAG, "The current projection is: $projection")
                }
                false
            }
            Column(
                modifier = Modifier.fillMaxSize()
            ) {
                Button(onClick = {
                    cameraPositionState.move(
                        update = CameraUpdateFactory.newLatLngBounds(bounds, 100)
                    )
                }) {
                    Text("Update Camera")
                }
                Box(Modifier.fillMaxSize()) {
                    LaunchedEffect(key1 = true ){
                        cameraPositionState.move(
                            update = CameraUpdateFactory.newLatLngBounds(bounds, 100)
                        )
                    }
                    GoogleMap(
                        modifier = Modifier.matchParentSize(),
                        googleMapOptionsFactory = {
                            GoogleMapOptions().mapId("DEMO_MAP_ID")
                        },
                        cameraPositionState = cameraPositionState,
                        properties = mapProperties,
                        onPOIClick = {
                            Log.d(TAG, "POI clicked: ${it.name}")
                        }
                    ) {

                        val textView = TextView(this@AdvancedMarkersActivity)
                        textView.text = "Hello!!"
                        textView.setBackgroundColor(Color.BLACK)
                        textView.setTextColor(Color.YELLOW)

                        AdvancedMarker(
                            state = marker4State,
                            onClick = markerClick,
                            collisionBehavior = 1,
                            iconView = textView,
                            title="Marker 4"
                        )

                        val pinConfig = PinConfig.builder()
                            .setBackgroundColor(Color.MAGENTA)
                            .setBorderColor(Color.WHITE)
                            .build()

                        AdvancedMarker(
                            state = marker1State,
                            onClick = markerClick,
                            collisionBehavior = 1,
                            pinConfig = pinConfig,
                            title="Marker 1"
                        )

                        val glyphOne = PinConfig.Glyph("A", Color.BLACK)
                        val pinConfig2 = PinConfig.builder()
                            .setGlyph(glyphOne)
                            .build()

                        AdvancedMarker(
                            state = marker2State,
                            onClick = markerClick,
                            collisionBehavior = 1,
                            pinConfig = pinConfig2,
                            title="Marker 2"
                        )

                        val glyphImage: Int = ic_menu_myplaces
                        val descriptor = BitmapDescriptorFactory.fromResource(glyphImage)
                        val pinConfig3 = PinConfig.builder()
                            .setGlyph(PinConfig.Glyph(descriptor))
                            .build()

                        AdvancedMarker(
                            state = marker3State,
                            onClick = markerClick,
                            collisionBehavior = 1,
                            pinConfig = pinConfig3,
                            title="Marker 3"
                        )

                    }
                }
            }

        }
    }

    override fun onMapsSdkInitialized(renderer: MapsInitializer.Renderer) {
        when (renderer) {
            MapsInitializer.Renderer.LATEST -> Log.d("MapsDemo", "The latest version of the renderer is used.")
            MapsInitializer.Renderer.LEGACY -> Log.d("MapsDemo", "The legacy version of the renderer is used.")
            else -> {}
        }
    }
}




相关问题
How to decide the current point reach on google map?

How to decide the current point reach on google map? I have a list of points (pickup points) of a route that I want to show in my google map with polyline. Now i have to get the current location of ...

Topographical or relief data in Map APIs

I was wondering if anyone knew of any map APIs that offer topographical or relief data? I ve had a quick look at Google and Bing APIs, but could find nothing there. Google allow you to view a map as ...

Using maps on Windows Mobile

I m experimenting with maps on different mobile platforms. Getting Google Maps to work on Android was easy, following this tutorial. Getting the same to work on Windows Mobile is a different matter. ...

Adding a custom icon to a google map

I need a hand adding a custom icon to some Google Maps javascript. Code below for your reference: function populateMap() { var map = new GMap2(document.getElementById("map")); map.setCenter(new ...

RSS to KML Overlay

I m want to display my blog as a Google Map overlay (each post contains geotags). How can I dynamically create a KML overlay from an RSS? Or better, how can I create a loop (PHP) that would display ...

开放街道地图管理员

我需要开放Street的标记管理员。 地图,如山角地图。

热门标签