2015-08-31 95 views
-1

我有許多標記的地圖。如何在調整地圖容器大小後居中放大Google地圖?

enter image description here

調整地圖後,它看起來像這樣:

enter image description here

,我想這一點:

enter image description here

我怎麼能放大和中心調整大小後在標記上映射?

我有以下代碼;

var map; 

var GoogleMaps = { 
    setMarkers: function(stores){ 
    map = new google.maps.Map(document.getElementById('map-canvas')); 

    $.each(stores, function (key, store) { 
     /* (..) */ 
     bounds.extend(latLng); 
    }); 

    map.fitBounds(bounds); 

    // This part is never triggered 
    google.maps.event.addDomListener(window, 'resize', function() { 
     var center = map.getCenter(); 
     google.maps.event.trigger(map, "resize"); 
     map.setCenter(center);  
    }); 
    } 
} 

HTML
當我點擊圖標調整大小,我添加了一個CSS類map-canvas

<div class="map-container alt-row row-spacer clearfix"> 
    <section class="column12"> 
    <div id="map-canvas" class=""></div> 
    </section> 
</div> 

我看過類似的問題(herehere),但他們不解決我的問題。

有誰知道我該如何做到這一點?你可以look at my JSFiddle here

+1

如果你downvote,至少評論爲什麼。 – Steven

回答

0

重新設置地圖,以適應當map DIV被調整的範圍應該工作:

google.maps.event.addDomListener(document.getElementById('map-canvas'), 'resize', function() { 
    google.maps.event.trigger(map, "resize"); 
    map.fitBounds(bounds); 
}); 
+0

問題是DOM偵聽器從未被觸發。我嘗試並添加了'console.log('test')' - 但沒有輸出。 – Steven

+0

我添加了地圖的HTML代碼。我認爲聽衆是錯誤的DOM元素。我嘗試了'google.maps.event.addDomListener($('。map-container'),'resize','但是這也不起作用) – Steven

+0

$('。map-container')一個DOM元素或DOM元素數組? – geocodezip

0

你可以嘗試的idle代替resize事件用於這一目的。

var map; 
 

 
var GoogleMaps = { 
 
    setMarkers: function (stores) { 
 
     map = new google.maps.Map(document.getElementById('map-canvas')); 
 

 
     var bounds = new google.maps.LatLngBounds(); 
 
     $.each(stores, function (key, store) { 
 

 
      var storePos = new google.maps.LatLng(store[1], store[2]); 
 

 
      var marker = new google.maps.Marker({ 
 
       position: storePos, 
 
       title: store[0], 
 
       map: map 
 
      }); 
 

 

 
      bounds.extend(storePos); 
 
     }); 
 

 
     map.fitBounds(bounds); 
 

 

 
     /*google.maps.event.addDomListener(map, 'resize', function() { 
 
      var center = map.getCenter(); 
 
      google.maps.event.trigger(map, "resize"); 
 
      map.setCenter(center); 
 
     });*/ 
 

 
     google.maps.event.addListener(map, 'idle', function (event) { 
 
      var center = map.getCenter(); 
 
      map.setCenter(center); 
 
      document.getElementById('result').innerHTML += 'Adjusted..'; 
 
     }); 
 
    } 
 
} 
 

 

 
google.maps.event.addDomListener(window, 'load', function() { 
 
    var stores = [ 
 
     ['0183 Oslo, Norja', 59.918816, 10.751814], 
 
     ['1154 Oslo, Norja', 59.873011, 10.814299], 
 
     ['0264 Oslo, Norja', 59.916751, 10.705809] 
 
    ]; 
 
    GoogleMaps.setMarkers(stores); 
 
});
html, body, #map-canvas { 
 
    height: 100%; 
 
    margin: 0px; 
 
    padding: 0px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
 
<div id="map-canvas"></div> 
 
<div id='result'/>

+0

謝謝,我不能讓它工作,嘗試使用'resize'和'idle'。你看看我的小提琴?http://jsfiddle.net/d7fykbzm/6/ – Steven

相關問題