2011-07-27 57 views
2

下面是我的代碼的精簡版,每2秒重新加載一次KML文件。每次更新時,我都希望讓相機平移到圖層的功能範圍,但我無法弄清楚設置回調的位置。OpenLayers http刷新回調函數

//set up ajax  
var protocol = new OpenLayers.Protocol.HTTP({ 
    url: "feed.kml", 
    format: new OpenLayers.Format.KML(), 
    callback: function(){ 
     alert("here"); //never called 
    } 
}); 

//create refresher 
var refresh = new OpenLayers.Strategy.Refresh({force: true, active: true}); 

//create layer for kml 
var trackLayer = new OpenLayers.Layer.Vector("KML", { 
    strategies: [new OpenLayers.Strategy.Fixed(), refresh], 
    protocol: protocol, 
}); 

//add to map 
map.addLayer(trackLayer); 

//refresh the kml every 2 seconds 
setInterval(function(){ 
    refresh.refresh();  
    //pan to extent 
    map.panTo(trackLayer.getDataExtent().getCenterLonLat()); 
},2000); 

問題是這行:map.panTo(trackLayer.getDataExtent().getCenterLonLat());在請求完成之前被調用。

這裏的文檔:

回答

2

NVM我知道了。我覺得沒有更快地理解這一點,我感到很蠢。

//zoom callback 
trackLayer.events.register("featuresadded",trackLayer,function(){ 
    var bounds = trackLayer.getDataExtent(); 
    if(bounds){ map.panTo(bounds.getCenterLonLat()); } 
});