gpt4 book ai didi

javascript - google maps v3 zoom to fit all markers(path) 功能

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:35:54 26 4
gpt4 key购买 nike

我在 js fiddle 上整理了一个小片段,这样您就可以看到我正在使用什么。

基本上,我正在尝试连接一个“缩放”按钮,这样一旦创建了路径,您就可以单击缩放按钮, map 就会缩放以适合该路径。我找到的所有答案都是通过一系列我没有的标记来工作的。任何建议将不胜感激。

http://jsfiddle.net/A3NBZ/

最佳答案

好吧,事实上您确实有一组标记!它存储在您在用户单击 map 时创建的 Polyline 中。要检索用户点击的点,只需使用 Polyline.getPath() .然后,您可以将这些点(如 geocodezip 所述)添加到 LatLngBounds 对象并使用 google.maps.Map.fitBounds()将 map View 调整到给定的边界。

这是缩放方法的简单实现,基于您提供的代码示例(您可以看到它在工作 here )。

function zoom() {
var bounds = new google.maps.LatLngBounds();
geodesic.getPath().forEach(function(latLng) {
bounds.extend(latLng);
});
map.fitBounds(bounds);
}

关于javascript - google maps v3 zoom to fit all markers(path) 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12678786/

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