2011-07-21 47 views
1

我有一個問題,我一直想弄清楚一段時間了。JQuery,谷歌地圖:「無法在初始化之前調用gmap上的方法」

有一段代碼可以正常工作,但如果我在其中添加一些JQuery,它會崩潰一切。

我得到以下錯誤:「未捕獲不能調用之前GMAP到初始化方法,試圖調用方法「addMarker」

的錯誤代碼是從‘$。每個(markersList.markers,’

任何幫助點我走出了正確的方向將不勝感激。謝謝!

function initialize() { 
var myOptions = { 
    zoom: 7, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

$.each(markersList.markers, 
     function(map, marker) { 
      var destination = (marker.lat + ', ' + marker.lng); 
      $('#map_canvas').gmap('addMarker', 
      { 'position': new google.maps.LatLng(marker.lat, marker.lng), 'title': marker.title }, function(map, marker) { 
       $('#map_canvas').gmap('addInfoWindow', 
        { 'content': null }, function(iw) { 
         $(marker).click(function() {iw.open(map, marker); 
          map.panTo(marker.getPosition()); 
          $('#to').val(destination); 
          alert("You've selected " + marker.title + " as your destination. Please enter the origin"); 
         }); 
       }); 
      }); 
     }); 

if (navigator.geolocation) { 
    browserSupportFlag = true; 
    navigator.geolocation.getCurrentPosition(function(position) { 
     initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     contentString = "Your location was found and has been added to starting point."; 
     map.setCenter(initialLocation); 
     $('#from').val(initialLocation); 
    }, function() { 
     handleNoGeolocation(browserSupportFlag); 
    }); 

回答

1

我有我的一個類似的問題,我所做的就是創建初始化函數從的其餘部分一個單獨的函數代碼,當頁面加載I CA在Initialize()函數的末尾,我調用searchLocations(),它將所有標記添加到地圖中。

相關問題