Google Maps fitBounds无法正常运行

use*_*820 7 javascript google-maps google-maps-api-3 fitbounds

我的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);
Run Code Online (Sandbox Code Playgroud)

某些图标将显示在视口/可见区域之外.

想法?

提前致谢.

Dan*_*llo 9

考虑以下示例,它将在美国东北部生成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>
Run Code Online (Sandbox Code Playgroud)

多次刷新此示例,没有标记出现在视口之外.最多,当隐藏在控件后面时,有时标记会从顶部略微剪裁:

谷歌地图LatLngBounds.extend()演示http://img825.imageshack.us/img825/7424/fitboundsfit.png

fitBounds()总是在LatLngBounds对象和视口之间留下小的余量也是没有价值的.这在下面的屏幕截图中清楚地显示,其中红色边界框表示LatLngBounds传递给fitBounds()方法的方框:

fitBounds Padding http://img204.imageshack.us/img204/9149/fitit.png

fitBounds Padding http://img441.imageshack.us/img441/9615/fitus.png

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


Mar*_*elo 0

向我们展示该患者的链接。国家数组中有多少个国家?整个世界?你的界限是否跨越了反经络?

Country.lat 和country.lng 是每个国家/地区的一个点,这不足以定义该国家/地区的边界框。这是某种“国家质心”吗?

如果是这种情况,并且您的标记位于最东国家/地区的质心以东或最西国家/地区的质心以西,那么这些标记当然会超出您定义的范围。

map.fitBounds()方法效果很好。:-)

马塞洛.