javascript - 我怎样才能为 map 上的所有形状设置边界?

标签 javascript google-maps-api-3 fitbounds

map 上有很多形状,它们都保存在这个数组array_shapes_object[]中。如何通过 fitBounds 方法在 map 上显示所有这些?

我可以使用类似的东西:

map.fitBounds(circle.getBounds());

map.fitBounds(rectangle.getBounds());

var points = new google.maps.LatLng(arrayLatitude[aa], arrayLongitude[aa]);
bounds.extend(points);
map.fitBounds(bounds);

但它们都只适用于一种形状,而且不会超过一种。 似乎只有三种形状。 (圆形、矩形、多边形)。

当我点击一个运行函数的按钮时,我想在 map 上调整所有形状。

最佳答案

您需要创建一个 google.maps.LatLngBounds对象,它是您要显示的所有对象的边界的并集。

union(other:LatLngBounds) | LatLngBounds | Extends this bounds to contain the union of this and the given bounds.

伪代码(需要把所有你想在 map 上显示的形状的bounds都加进去):

var bounds = circle.getBounds();
bounds.union(rectangle.getBounds();
for (var aa=0; aa < arrayLatitude.length; aa++) {
  var points = new google.maps.LatLng(arrayLatitude[aa], arrayLongitude[aa]);
  bounds.extend(points);
}
map.fitBounds(bounds); 

关于javascript - 我怎样才能为 map 上的所有形状设置边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19887218/

相关文章:

javascript - Vue表单未提交

javascript - 转换点符号 json

jquery - 尝试清除所有标记时,谷歌地图 v3 setMap 未定义

javascript - 引用错误 : google is not defined using ng-map GeoCoder service

javascript - 如何使用谷歌地图中的地址数组显示信息窗口和折线?

javascript - 如何使用 fitBounds() 覆盖 Google map 初始化的选项;

javascript - 动态 li 标签内按钮的 onclick 事件未定义

javascript - 根据 React 中不同字段的不同文本输入进行过滤

google-maps - Google map v3 fitBounds() 对于单个标记缩放太近

javascript - Google map JavaScript API - fitbounds 与 setCenter 一起使用