Javascript 谷歌地图不能正常工作

Javascript 谷歌地图不能正常工作,javascript,google-maps,google-maps-api-3,fitbounds,Javascript,Google Maps,Google Maps Api 3,Fitbounds,我对googlemaps函数有一个问题 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); for(变量i

我对googlemaps函数有一个问题

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);
for(变量i=0;i
某些图标将显示在视口/可见区域之外

你有什么想法


提前感谢。

向我们显示患者的链接。国家/地区数组中有多少个国家/地区?整个世界?你的边界是否越过反子午线

country.lat和country.lng是每个国家一个点,这还不足以定义国家的边界框。这是某种“国家中心”吗

如果是这样的话,如果你在最东端国家的质心以东或最西端国家的质心以西有标记,那么这些标记当然会超出你定义的范围

map.fitBounds()
方法工作正常。:-)


Marcelo.

考虑以下示例,该示例将在美国东北部生成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>

Google Maps LatLngBounds.extend()演示
var map=new google.maps.map(document.getElementById('map'),{
mapTypeId:google.maps.mapTypeId.TERRAIN
});
var markerBounds=new google.maps.LatLngBounds();
var随机点,i;
对于(i=0;i<10;i++){
//在美国东北部生成10个随机点
randomPoint=新的google.maps.LatLng(39.00+(Math.random()-0.5)*20,
-77.00+(数学随机数()-0.5)*20);
//为每个随机点绘制一个标记
新的google.maps.Marker({
位置:随机点,
地图:地图
});
//使用每个随机点扩展markerBounds。
扩展标记边界(随机点);
}
//最后,markerBounds将是要包含的最小边界框
//我们的10个随机点
//最后,我们可以调用Map.fitBounds()方法将映射设置为适合
//我们的markerBounds
地图边界(markerBounds);
多次刷新此示例后,任何标记都不会超出视口。有时,当标记隐藏在控件后面时,有时会从顶部稍微剪掉:

fitBounds()
始终在
LatLngBounds
对象和视口之间留有一小段空白,这也没有什么价值。下面的屏幕截图清楚地显示了这一点,其中红色边界框表示传递给
fitBounds()
方法的
LatLngBounds

您还可能有兴趣查看以下有关此主题的堆栈溢出帖子:


检查您的谷歌地图对象是否正确显示在您指定的区域。 谷歌地图对象的某些部分可能溢出到它的容器外,而标记位于该区域内,它们存在于你看不见的地方