我正在尝试找出一种在谷歌地图处理和呈现 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 语句的好条件。
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();
}