gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:26:57 25 4
gpt4 key购买 nike

当访问者单击某个站点时,它会显示该位置的街景 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com