【发布时间】:2011-08-28 06:49:40
【问题描述】:
我正在寻找一种方法来使用 Google Maps V3 API 计算给定边界的缩放级别,类似于 V2 API 中的getBoundsZoomLevel()。
这是我想做的:
// These are exact bounds previously captured from the map object
var sw = new google.maps.LatLng(42.763479, -84.338918);
var ne = new google.maps.LatLng(42.679488, -84.524313);
var bounds = new google.maps.LatLngBounds(sw, ne);
var zoom = // do some magic to calculate the zoom level
// Set the map to these exact bounds
map.setCenter(bounds.getCenter());
map.setZoom(zoom);
// NOTE: fitBounds() will not work
不幸的是,我不能将fitBounds() 方法用于我的特定用例。它适用于在地图上拟合标记,但不适用于设置精确的边界。这是我为什么不能使用fitBounds() 方法的示例。
map.fitBounds(map.getBounds()); // not what you expect
【问题讨论】:
-
最后一个例子非常好,非常有说明性! +1。我有same problem。
-
对不起,链接错误的问题,this is the correct link。
-
这个问题不是the other question的重复问题。另一个问题的答案是使用
fitBounds()。这个问题询问当fitBounds()不足时该怎么办 - 因为它过度缩放或者您不想缩放(即,您只想要缩放级别)。 -
@Nick Clark:你怎么知道要设置的 sw、ne 边界?您之前是如何捕获它们的?
标签: google-maps google-maps-api-3