Google 地图 - 缩放到多边形
Google Maps - zoom to polygon
我检查了几个问题(this one, this one and this one)
关于将 google 地图缩放到 Android 中给定的 Polygon
或 List<LatLng>
,但我找不到答案。
函数是什么
public static int getZoomLevelForPolygon(final List<LatLng> listOfPolygonCoordinates)
长什么样?我可以将地图缩放到某个多边形吗?
好的,我想我设法找到了解决方案:
首先,我们生成一个适合多边形的最小矩形,也称为 LatLngBounds
对象。然后,我们移动相机以适应作为参数提供的 LatLngBounds
。
主要调用:
final int POLYGON_PADDING_PREFERENCE = 200;
final LatLngBounds latLngBounds = getPolygonLatLngBounds(polygon);
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, POLYGON_PADDING_PREFERENCE));
辅助函数:
private static LatLngBounds getPolygonLatLngBounds(final List<LatLng> polygon) {
final LatLngBounds.Builder centerBuilder = LatLngBounds.builder();
for (LatLng point : polygon) {
centerBuilder.include(point);
}
return centerBuilder.build();
}
我检查了几个问题(this one, this one and this one)
关于将 google 地图缩放到 Android 中给定的 Polygon
或 List<LatLng>
,但我找不到答案。
函数是什么
public static int getZoomLevelForPolygon(final List<LatLng> listOfPolygonCoordinates)
长什么样?我可以将地图缩放到某个多边形吗?
好的,我想我设法找到了解决方案:
首先,我们生成一个适合多边形的最小矩形,也称为 LatLngBounds
对象。然后,我们移动相机以适应作为参数提供的 LatLngBounds
。
主要调用:
final int POLYGON_PADDING_PREFERENCE = 200;
final LatLngBounds latLngBounds = getPolygonLatLngBounds(polygon);
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, POLYGON_PADDING_PREFERENCE));
辅助函数:
private static LatLngBounds getPolygonLatLngBounds(final List<LatLng> polygon) {
final LatLngBounds.Builder centerBuilder = LatLngBounds.builder();
for (LatLng point : polygon) {
centerBuilder.include(point);
}
return centerBuilder.build();
}