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 -> {}
}
}
}