2013-04-06 117 views
0

我按照我的標準做得很好!除了函數的基礎知識之外,我對JS的知識幾乎是零。我使用這些頁面將一個可以使用SimpleModal框架將Google Maps加載到Modal中的工作腳本。爲了我的安慰,我得到了它的工作,但它有一個最後的錯誤,我不能轉移。模態在第一次點擊HREF時加載,但是如果我關閉了模態,然後嘗試重新打開它,它會加載缺失地圖部分的模態。缺失的地圖問題是我認爲我已經解決的一個問題。我的JS是Simplemodal Google地圖只顯示一次,但不顯示第二次點擊

  var map; 
     var src = 'https://sites.google.com/site/bristol2monaco/kml/route2.kml'; 
     function initialize() { 
     var myLatlng = new google.maps.LatLng(51.337890,-0.813049); 
     map = new google.maps.Map(document.getElementById("basic-modal-content"), { 
     center: myLatlng, 
     zoom: 7, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 
     loadKmlLayer(src, map); 
    } 

     function loadKmlLayer(src, map) { 
     var kmlLayer = new google.maps.KmlLayer(src, { 
     suppressInfoWindows: true, 
     clickable: false, 
     preserveViewport: true, 
     map: map 
     }); 
    } 
    initialize(); 

和註冊的js文件「點擊」載:

jQuery(function ($) { 
// Load dialog on page load 
//$('#basic-modal-content').modal(); 

// Load dialog on click 
$('#table .newbasic').click(function (e) { 
    $('#basic-modal-content').modal(); 
    var center = map.getCenter(); 
    google.maps.event.trigger(map, "resize"); 
    map.setCenter(center); 
    return false; 
}); 
}); 

,因爲我以爲我已經解決了缺少地圖的bug(貼在這裏使用的解決方案)與(圖,調整大小)線以上沒有任何解決方案在這裏幫助。我必須重新初始化地圖嗎?感謝您的建議。

+0

嘗試重置邊界。 'map.fitBounds(map.getBounds());' – 2013-04-06 10:33:42

+0

我在map.setCenter行後試過你的代碼,它沒有工作,是插入它的正確位置?感謝您及時的回覆。 – user2205626 2013-04-06 10:54:22

+0

這應該沒問題。修復了一個類似的問題,雖然我重新應用了我的標記邊界。如果你可以把一個[jsfiddle](http://jsfiddle.net/)放在一起,其他人可能會提供幫助。 – 2013-04-06 13:10:17

回答

0

當您調用模式打開時,請使用Eric Martin描述的onOpen函數。通過使用onOpen函數,您將能夠使用回調功能,並因此使用Google地圖事件偵聽器來偵聽調整大小事件。一旦聽到重新調整大小事件,您可以重新初始化您的谷歌地圖,從而刪除灰色區域

$("#table .newbasic").modal({ 
     onOpen: function (dialog) { 
      google.maps.event.addListenerOnce(map, 'resize', function() { 
      //Alert TESTING IF RESIZE is heard(remove after test) 
      alert("heard resize onOpen"); 
      initialize();     
      map.setCenter(center); 
      }); 
      google.maps.event.trigger(map, "resize"); 
     } 
    }); 
相關問題