2010-08-04 106 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清楚地顯示在話題溢出帖子: