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());
在請求完成之前被調用。
這裏的文檔:
- http://dev.openlayers.org/docs/files/OpenLayers/Strategy/Refresh-js.html
- http://dev.openlayers.org/docs/files/OpenLayers/Layer/Vector-js.html
- http://dev.openlayers.org/docs/files/OpenLayers/Protocol/HTTP-js.html