提问人:user22340184 提问时间:11/8/2023 最后编辑:user22340184 更新时间:11/20/2023 访问量:37
如何将 Android 版 Google 地图中的可见区域放入更小的框中?
How to fit the visible area in Google Maps on Android into a smaller box?
问:
我的屏幕上有一张带有路线的地图,还有一个抽屉。当我拉起抽屉以覆盖屏幕的 50%(近似数字)时,我希望我的地图缩小并向上移动,以便用户仍然可以看到路线。
我试过什么:
- 移动地图,但我不知道缩放数:
// Calculate the dimensions of the visible portion
val visibleWidth = binding.mapView.width
val visibleHeight = binding.mapView.height - binding.bottomSheet.root.height
// Get the LatLng coordinates of the visible region
val visibleTopLeft = googleMap.projection.fromScreenLocation(Point(0, 0));
val visibleBottomRight = googleMap.projection.fromScreenLocation(Point(visibleWidth, visibleHeight))
// Calculate the new camera position based on the visible region
val newCenter = LatLng(
(visibleTopLeft.latitude + visibleBottomRight.latitude) / 2,
(visibleTopLeft.longitude + visibleBottomRight.longitude) / 2
)
val newZoomLevel = googleMap.cameraPosition.zoom //this should be something else
// Create a new CameraPosition and update the camera
val cameraPosition = CameraPosition.Builder()
.target(newCenter)
.zoom(newZoomLevel)
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
- 再次添加路线坐标,并设置 CameraFactory.newLatLngBounds() 宽度和高度以及填充:
val builder = LatLngBounds.builder()
coordinates.forEach { builder.include(it) }
googleMap.moveCamera(
CameraUpdateFactory.newLatLngBounds(
builder.build(),
binding.root.width.toPx(),
calculatedHeight, //this doesn't work
100.toPx()
)
)
答: 暂无答案
评论