2012-12-04 71 views
0

我試圖找出一種方式來運行進度加載器,而谷歌地圖進程和呈現一個kml。谷歌地圖功能之前聽衆

我有一個監聽設置status_changed。

google.maps.event.addListener(kmllayer, "status_changed", function() { 
    var kmlstatus = kmllayer.getStatus(); 
    if (kmlstatus != 'OK') { 
     $('kmllayerlist').append($('<div class="kmllayeritem">Error Loading KML' + kmlstatus +'</div>')); 
    } else { 
     var itemhostname = kmllayerURL.split("/"); 
     $('#kmllayerlist').append($('<div class="kmllayeritem"><input type="checkbox" class="kmllayerchx" CHECKED><img src="http://www.google.com/s2/favicons?domain='+itemhostname[2]+'" class="kmllisticon" alt="kmlurlicon">'+itemhostname[(itemhostname.length - 1)]+'</input></div>')); 
     kmllayerarr.push(kmllayerURL); 
    } 
}); 

如何讓一個函數運行直到偵聽器被觸發?我在想像下面這樣的東西,但我想不出一個好的條件來打破這個時間表。

while() { 
    //function to run until listener fires 
} 

工作代碼塊

function addkmlLayer_m(kmllayerURL) { 
kmllayer = new google.maps.KmlLayer(kmllayerURL, { 
    preserveViewport: true 
}); 
kmllayer.setMap(map); 
var itemhostname = kmllayerURL.split("/"); 
var kmlstatus_boo; 
function kmlProgress() { 
    if (kmlstatus_boo != 'done') { 
     google.maps.event.addListener(kmllayer, "status_changed", function() { 
      kmlstatus_boo = 'done'; 
      $('#progress').remove(); 
      kmlstatus = kmllayer.getStatus(); 
      if (kmlstatus != 'OK') { 
       $('#kmllayerlist').append($('<div id="kmllayeritem_error" class="kmllayeritem">Error Loading KML ' + kmlstatus +'<input class="kmldel" type="image" src="../images/delete.png" name="delete kml" OnClick="delkmlerr();" /></div>')); 
      } else { 
       kmllayerarr.push(kmllayer); 

       $('#kmllayerlist').append($('<div id="kmllayeritem_'+kmllayerarr.length+'" class="kmllayeritem"><input type="checkbox" class="kmllayerchx" CHECKED><img src="http://www.google.com/s2/favicons?domain='+itemhostname[2]+'" class="kmllisticon" alt="kmlurlicon">'+itemhostname[(itemhostname.length - 1)]+'</input><input class="kmldel" type="image" src="../images/delete.png" name="delete kml" OnClick="delkmlitem('+kmllayerarr.length+');" /></div>')); 
      } 
     }); 
     $('#kmllayerlist').append($('<div/>', {'id':'progress','class':'kmllayeritem','text':'Loading '+itemhostname[(itemhostname.length - 1)]+' KML'})); 
     kmlstatus_boo = null; // free the closure 
    } else { 
     window.setInterval(kmlProgress,400); // Run again in 400ms changed so the user actually sees the loading image. 
    } 
} 
kmlProgress(); 

}

回答

1

不幸的是,如果你運行像一個while循環,等待聽者火,它永遠不會發生。 Javascript是單線程的。因此,您需要放棄瀏覽器的時間,以便它能夠完成工作。試試沿着這些線路:

var myRelevantValue = "something"; 
function myTimer() { 
    if (myRelevantValue == "done") { 
     // It changed. 
     myRelevantValue = null; // free the closure 
    } else { 
     // Update progress bar 
     window.setInterval(myTimer,100); // Run again in 100ms 
    } 
} 
window.setInterval(myTimer,100); // Run in 100ms 

裏面你的聽衆,你的變量的值設置爲一些有用的東西,而當它發生時,計時器將抓住它。

+0

我覺得這個方法有潛力。您是否建議我將偵聽器放入myTimer函數中,並且偵聽器未被觸發(myRelevantValue =「False」),然後使用else運行progrress加載器?我會試試這個。 – normalhabit

+0

我用這種方法和@ sheikh-heera說的一些創建解決方案。關鍵是將status_change監聽程序放入timerfunction中。 – normalhabit

+0

偵聽器將在myTimer函數之外。它只需要設置一次,並且只要有事情發生變化就會多次觸發。如果你把監聽器放在裏面,那麼每當定時器觸發時你都會增加一個監聽器,這是你不想要的。我沒有在谷歌地圖api中使用kml,所以我不確定那裏的細節,但它應該是微不足道的,以便觀察你需要它觀察的變量。請記住在完成清除閉包時將它們設置爲空。 –

0

您可以設置一個帶啓動和停止參數的功能。

function progressLoader(status){ 
    if(status == 'start'){ 
     //show progress bar 
    } 
    if(status == 'stop'){ 
     //hide progress bar 
    } 
} 

然後就

google.maps.event.addListener(kmllayer, "status_changed", function() { 
var kmlstatus = kmllayer.getStatus(); 
if (kmlstatus != 'OK') { 
    progressLoader(start); 
    $('kmllayerlist').append($('<div class="kmllayeritem">Error Loading KML' + kmlstatus +'</div>')); 
} else { 
    var itemhostname = kmllayerURL.split("/"); 
    $('#kmllayerlist').append($('<div class="kmllayeritem"><input type="checkbox" class="kmllayerchx" CHECKED><img src="http://www.google.com/s2/favicons?domain='+itemhostname[2]+'" class="kmllisticon" alt="kmlurlicon">'+itemhostname[(itemhostname.length - 1)]+'</input></div>')); 
    kmllayerarr.push(kmllayerURL); 
    progressLoader(stop); 
} 
}); 
0

你可以試試這個

google.maps.event.addListener(kmllayer, "status_changed", function() { 
    $('kmllayerlist').append($('<div/>', {'id':'progress','text':'Loading...', 'style':'color:black;font-weight:bold'})); // style can be changed 
    var kmlstatus = kmllayer.getStatus(); 
    if (kmlstatus != 'OK') 
    { 
     $('#progress').remove(); 
     $('kmllayerlist').append($('<div class="kmllayeritem">Error Loading KML' + kmlstatus +'</div>')); 
    } 
    else 
    { 
     var itemhostname = kmllayerURL.split("/"); 
     $('#progress').remove(); 
     $('#kmllayerlist').append($('<div class="kmllayeritem"><input type="checkbox" class="kmllayerchx" CHECKED><img src="http://www.google.com/s2/favicons?domain='+itemhostname[2]+'" class="kmllisticon" alt="kmlurlicon">'+itemhostname[(itemhostname.length - 1)]+'</input></div>')); 
     kmllayerarr.push(kmllayerURL); 
    } 
});