OpenLayers http刷新回调函数

时间:2011-07-27 15:13:31

标签: javascript openlayers

这是我的代码的精简版本,每2秒重新加载一个KML文件。在每次更新时,我想让摄像机平移到图层的功能范围,但我无法确定在何处设置回调。

//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());在请求完成之前被调用。

这是文档:

1 个答案:

答案 0 :(得分:2)

Nvm我明白了。因为没有更快地搞清楚这一点我感到愚蠢。

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