我对 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);
一些图标将显示在视口/可见区域之外。
和想法?
提前致谢。
考虑以下示例,它将在美国东北部生成 10 个随机点,并应用fitBounds()
method.
<!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>
多次刷新此示例,没有任何标记会超出视口。至多,有时当标记隐藏在控件后面时,标记会从顶部稍微被剪掉:
这也是毫无价值的fitBounds()
之间总是留有很小的余量LatLngBounds
对象和视口。下面的屏幕截图清楚地显示了这一点,其中红色边框代表LatLngBounds
它被传递给fitBounds()
method:
您可能还有兴趣查看以下有关该主题的 Stack Overflow 帖子:
- Google 地图 api V3 中的 fitbounds() 不适合边界 https://stackoverflow.com/questions/2494756/fitbounds-in-google-maps-api-v3-does-not-fit-bounds
- Google 地图 v3 - 自动缩放级别? https://stackoverflow.com/questions/3245564/google-maps-javascript-api-automate-zoom-level
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)