2012-06-26 21 views
2

我正在使用goMap jQuery插件集成Google Maps API v3。我想重繪地圖時遇到問題。我有2個輸入框用於輸入緯度和經度,然後觸發一個文本鏈接goMap javascript代碼...當這被稱爲第一次地圖渲染正常,但如果我改變緯度/經度值並再次調用相同的代碼重繪新座標,在地圖的心不是...我的代碼看起來像goMap jquery插件重繪地圖

<input id="form_office_latitude" class="span4 office_latitude" type="text" name="office_latitude" value=""> 
<input id="form_office_longitude" class="span4 office_longitude" type="text" name="office_longitude" value=""> 
<a class="check-map" href="javascript:{};">Preveri zemljevid</a> 

$('.check-map').live('click', function(){ 
     $("#map_canvas").goMap({ 
      maptype: 'ROADMAP', 
      zoom: 15, 
      markers: [{ 
       icon: base_url+'assets/img/marker.png', 
       latitude: $('.office_latitude').val(), 
       longitude: $('.office_longitude').val(), 
       id: 'test', 
       html: { 
        content: 'Hello Word!', 
        popup: true 
       } 
      }], 
      hideByClick: false 
     }); 
}); 

回答

4

我不是在goMap插件方面的專家,但你可能不希望重繪或重新創建地圖就像你現在是(建立一個全新的地圖)。相反,你想重新使用像這樣的地圖:

var center = new google.maps.LatLng($('.office_latitude').val(), 
            $('.office_longitude').val()); 
$.goMap.map.setCenter(center); 
+0

但如何檢查地圖是否已經呈現? –

+0

,調用$ .goMap.map.setCenter(center);還有另一個問題。如果你在一個頁面中有多個地圖實例(就像我這樣做),那麼調用$ .goMap.map.setCenter(center)將不會使我想要的地圖居中...... –

+0

更新:沒關係我想通了。 goMap知道它是否已經被渲染。你的回答是對的!更好的方法是調用'$ .goMap.map.createMarker(markerOptions);'這將創建標記並根據markerOptions將地圖居中。 –