2010-07-09 24 views
1

即時通訊工作在JavaScript(v3)中的Google地圖上。 我需要顯示一些來自XML的標記,因爲我使用Jquery。在jquery.each()上調用fitbounds的問題()

繼承人的對象和功能,可能會救我的時間來解釋:

var VX = { 
    map:null, 
    bounds:null 
} 
VX.placeMarkers = function(filename) { 
    $.get(filename, function(xml) { 
     $(xml).find("marker").each(function() { 
      var lat   = $(this).find('lat').text(); 
      var lng   = $(this).find('lng').text(); 
      var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng)); 
      VX.bounds.extend(point);     
      VX.map.fitBounds(VX.bounds); //this works   
      var marker = new google.maps.Marker({ 
       position: point, 
       map: VX.map, 
       zoom: 10, 
       center: point 
       }); 
      }); 
     }); 
     //VX.map.fitBounds(VX.bounds);  //this shows me the ocean east of africa  
} 

所以basicaly我的問題是,我無法弄清楚如何從功能。每個外面做fitbounds,做它裏面的函數調用它看起來很糟糕的每個標記。

我聲明瞭界限,當我初始化地圖時......未包含整個代碼,因爲它像300行。

肩膀我能夠使用我傳遞給全局對象的值嗎?

編輯:啊,我從get函數以外調用它!

+1

我沒有看到你發佈的代碼是如何工作的。 'VX'沒有'bounds.extend'或'map.fitBounds'屬性。 – 2010-07-09 19:17:14

+0

這些都是谷歌地圖API函數 – Stjerneklar 2010-07-10 09:52:16

回答

0

第二個調用不起作用,因爲它在ajax get()返回之前觸發。

fitBounds放在get()處理程序中,但在each()函數之外。像這樣:

var VX = { 
    map:null, 
    bounds:null 
} 
VX.placeMarkers = function(filename) 
{ 
    $.get 
    (
     filename, 
     function(xml) 
     { 
      $(xml).find("marker").each 
      (
       function() 
       { 
        var lat   = $(this).find('lat').text(); 
        var lng   = $(this).find('lng').text(); 
        var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng)); 

        VX.bounds.extend(point);     
        //VX.map.fitBounds(VX.bounds); //this works   

        var marker = new google.maps.Marker 
        ({ 
         position: point, 
         map: VX.map, 
         zoom: 10, 
         center: point 
        }); 
       } 
      ); 
      VX.map.fitBounds(VX.bounds); //-- This should work. 
     } 
    ); 
    //VX.map.fitBounds(VX.bounds);  //this shows me the ocean east of africa  
} 
+0

哇,這就是它!感謝:D 希望我代表upvote你:) – Stjerneklar 2010-07-09 20:43:39