javascript - 有没有办法告诉谷歌地图以标记为中心缩小并显示计算出的边界框?

标签 javascript google-maps google-maps-markers

当访问者单击某个站点时,它会显示该位置的街景 View 。当他们退出图片时,代码会获取最近的两个标记坐标(来自 mongodb)并缩小 map 以包含计算出的边界框。

我希望拥有它,以便 map 保持以刚刚访问的标记为中心,并缩小到足以显示完整的边界框。这将帮助用户跟踪他们刚刚去过的地方。

Click the image to calculate the bounding box

该图像的标记位于最左侧。正如您所看到的,边界框居中。

enter image description here

我已经通读过,但对我来说似乎没有什么是显而易见的。 Google API V3 有这个方法吗?

/****
COLLECT Data from RESTful Service
This code gets the nearest two markers and calculates the zoom to set the map to include them in view.
****/
bounds = new google.maps.LatLngBounds();
bounds.extend(new google.maps.LatLng( data.locs.lat, data.locs.lng ));
nearest = {};
$.ajax({
    type: "GET",
    dataType: "json",
    data: { lat : data.locs.lat,  //the marker you are viewing 
            lng : data.locs.lng},
    async: false,
    url: "php/restful.get.nearest.markers.php",
    success: function (data) {
        nearest = data;  
    }
});
$.each(nearest, function () 
{
    bounds.extend(new google.maps.LatLng( this.locs.lat, this.locs.lng ));
});
/****
END COLLECT Data from RESTful Service
****/
map.fitBounds(bounds);  
new google.maps.Rectangle({
    bounds: bounds,
    map: map,
    fillColor: "#000000",
    fillOpacity: 0.2,
    strokeWeight: 0
}); 

最佳答案

这可能不是最简单的方法,但它可以作为一个开始。

查看 Google Maps API 中的 MapOptions 。您将看到一个名为“缩放”的属性,从底部数第三个,它控制您的 map 缩放(显然)。

然后 map 的methods中有两个方法getZoom和setZoom它可用于以编程方式控制 map 的缩放级别。

您必须尝试这些,当然 setCenter 不应该是大问题。

实验并找出缩放级别覆盖的范围(以距离为单位),计算边界框覆盖的范围,并相应地设置缩放级别和中心。

关于javascript - 有没有办法告诉谷歌地图以标记为中心缩小并显示计算出的边界框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22291091/

相关文章:

javascript - react : setState is not rendering the page again

android - Android 中的地理围栏问题

javascript - 无法通过传递经纬度数组来设置折线的路径

javascript - Google map 信息窗口内容在重新加载时消失。可能是什么原因造成的?

ios - React Native map 上的标记单击事件在 React iOS 中不起作用

javascript - 使用 Google map v3 在特定地址上放置图钉

javascript - 如何删除谷歌地图上的标记?

javascript - 如何通过 Context API 使全局可访问的信息保存在本地以便在刷新时持续存在?

javascript - 如何通过javascript重置firefox浏览器中的表单

javascript - 如何通过 jQuery 动态设置日期的有限范围