将谷歌地图制作成一定的距离(米)



有人遇到过设置谷歌地图缩放级别以使窗口保持一定距离的方法吗?

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

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

  3. 这样做,直到计算出的宽度变得比目标宽度更小或更大,并计算出需要相乘或除法的频率。

  4. 将您计数的数字添加到当前缩放值中,以获得目标缩放值。

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

我通过创建一个具有给定距离的圆并使用它来获得边界,实现了类似的事情。由于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);

最新更新