3

有没有人遇到过设置谷歌地图缩放级别的方法,以便窗口有一定的距离?

4

2 回答 2

3
  1. 在当前缩放级别使用中测量地图的宽度google.maps.geometry.spherical.computeDistanceBetween()map.getBounds()(请参阅doku)。

  2. 如果需要时测量距离更宽,请将其除以 2。如果它更小,则乘以 2。

  3. 这样做直到计算出的宽度变得比目标宽度更小或更大,并计算你必须多久进行一次乘法或除法。

  4. 将您计算的数字添加/减去当前缩放值以获得目标缩放值。

注意:您无法将地图调整到精确的宽度,因为您受限于固定的缩放级别,因此这只是一个近似值。

于 2012-08-21T14:16:17.000 回答
1

我通过创建一个具有给定距离的圆并使用它来获得边界来实现类似的事情。地图的宽度与圆圈不太一样,因为 API 添加了一些填充。

//Accuracy in metres
var accuracy = 50; 

/* Create a circle but don't set the 'map' variable as we don't need to see circle on the map */
var circle = new google.maps.Circle({
                 center: map.getCenter(),
                 radius: accuracy
             });

//Get the new bounds from the circle
var bounds = circle.getBounds();

/* Fit the map to the size of the circle. Will be slightly bigger than the circle as the API adds a little padding */
map.fitBounds(bounds);
于 2013-09-29T13:37:14.777 回答