2010-08-04 99 views
7

我有一个googlemaps fitBounds函数的问题。谷歌地图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); 

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

和想法?

在此先感谢。

回答

0

告诉我们一个链接到病人。你在这个国家有多少个国家?整个世界?你的边界是否穿过反经络?

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

如果是这种情况,并且如果您在最东端国家的质心以东,或者在西方国家的重心的西边标记了标记,那么这些标记当然会超出您的范围定义。

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

Marcelo。

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> 

刷新此示例很多次,没有标记出现在视口外。至多,有时标记稍微从顶部时,隐藏在背后的控制修剪:

Google Maps LatLngBounds.extend() Demo http://img825.imageshack.us/img825/7424/fitboundsfit.png

,也是一文不值的fitBounds()永远离开LatLngBounds对象和视口之间小幅度。

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

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

您还可能有兴趣在检查出以下堆栈:这是在下面的截图,其中红色边框代表传递给fitBounds()方法LatLngBounds清楚地显示在话题溢出帖子: