2013-04-06 64 views
0

我有一個問題,我不認爲fitBounds調用適當地設置縮放級別。我已經設法在這裏重現我的測試用例http://tom.haddons.net/map.html - 基本上四個外部點是我設置的界限,如果您加載頁面,然後放大一個級別,它們仍然可見。那麼爲什麼不是谷歌地圖設置縮放級別呢?谷歌地圖API fitBounds不能正確設置縮放

謝謝,湯姆

+0

[看起來像](http://www.geocodezip.com/v3_Bounds_tom_haddons_net_mapB.html)頂部有邊距,所以「默認」標記顯示 – geocodezip 2013-04-06 16:36:08

+0

[如何影響地圖的「寬限邊緣」 .fitBounds()](http://stackoverflow.com/questions/9843732/how-to-affect-the-grace-margin-of-map-fitbounds) – geocodezip 2013-04-06 16:44:09

回答

0

我設法通過執行以下操作來解決此問題:

var minlatminlong = new google.maps.LatLng({{ min_latitude }}, {{ min_longitude }}); 
    var minlatmaxlong = new google.maps.LatLng({{ min_latitude }}, {{ max_longitude }}); 
    var maxlatminlong = new google.maps.LatLng({{ max_latitude }}, {{ min_longitude }}); 
    var maxlatmaxlong = new google.maps.LatLng({{ max_latitude }}, {{ max_longitude }}); 

    zoomChangeBoundsListener = google.maps.event.addListener(map, 'bounds_changed', function(){ 
     var bounds = this.getBounds(); 
     if ((bounds.getSouthWest().lng() > {{ min_longitude }}) || (bounds.getNorthEast().lng() < {{ max_longitude }}) || (bounds.getSouthWest().lat() > {{ min_latitude }}) || (bounds.getNorthEast().lat() < {{ max_latitude }})) { 
      this.setZoom(this.getZoom() - 1); 
     } else { 
      google.maps.event.removeListener(zoomChangeBoundsListener); 
     } 
    }); 

注意,任何包含在「{{」和「}}」是我傳遞一個變量進入html模板。