如何将 PolyLine Fit 设置为我们的 Google 地图片段

How to set PolyLine Fit to our Google Map Fragment

我的多段线失去焦点。我正在使用 map.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 10)); 但这不是解决方案。如何解决这个问题

要解决您的问题,请先了解一下google 地图相机的工作原理。当你这样做时:

map.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 10));

相机将起始位置聚焦在中间,缩放级别为10。我们需要的是聚焦路线。为此,我们需要使用 latLng 边界。一旦我们用两个或多个 latLngs 创建了一个边界,我们就可以将它设置为地图的相机。这样您的用户就可以看到您的路线。

使用路线上的所有起点和终点标记创建边界:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(startMarker);
builder.include(endMarker);
LatLngBounds bounds = builder.build();

然后通过工厂获取运动描述对象:CameraUpdateFactory:

int padding = 0; // padding around start and end marker
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
googleMap.animateCamera(cu);