gpt4 book ai didi

javascript - 谷歌地图 fitBounds 无法正常工作

转载 作者:数据小太阳 更新时间:2023-10-29 04:37:44 25 4
gpt4 key购买 nike

我对 googlemaps fitBounds 函数有疑问。

for (var i = 0; i < countries.length; i++) {
var country = countries[i];
var latlng = new google.maps.LatLng(parseFloat(country.lat), parseFloat(country.lng));
mapBounds.extend(latlng);
}

map.fitBounds(mapBounds);

一些图标将显示在视口(viewport)/可见区域之外。

还有想法?

提前致谢。

最佳答案

考虑以下示例,它将在美国东北部生成 10 个随机点,并应用 fitBounds() 方法。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps LatLngBounds.extend() Demo</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 400px; height: 300px;"></div>

<script type="text/javascript">

var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.TERRAIN
});

var markerBounds = new google.maps.LatLngBounds();

var randomPoint, i;

for (i = 0; i < 10; i++) {
// Generate 10 random points within North East USA
randomPoint = new google.maps.LatLng( 39.00 + (Math.random() - 0.5) * 20,
-77.00 + (Math.random() - 0.5) * 20);

// Draw a marker for each random point
new google.maps.Marker({
position: randomPoint,
map: map
});

// Extend markerBounds with each random point.
markerBounds.extend(randomPoint);
}

// At the end markerBounds will be the smallest bounding box to contain
// our 10 random points

// Finally we can call the Map.fitBounds() method to set the map to fit
// our markerBounds
map.fitBounds(markerBounds);

</script>
</body>
</html>

多次刷新此示例,没有任何标记超出视口(viewport)。最多,有时标记隐藏在控件后面时会从顶部略微剪掉:

Google Maps LatLngBounds.extend() Demo

fitBounds() 总是在 LatLngBounds 对象和视口(viewport)之间留出一小段空白也是毫无意义的。下面的屏幕截图清楚地显示了这一点,其中红色边界框表示传递给 fitBounds() 方法的 LatLngBounds:

fitBounds Padding

fitBounds Padding

您可能还有兴趣查看以下有关该主题的 Stack Overflow 帖子:

关于javascript - 谷歌地图 fitBounds 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3407723/

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