开发者

OpenLayers http refresh callback function

开发者 https://www.devze.com 2023-03-23 02:01 出处:网络
Here\'s a stripped down version of my code that reloads a KML file every 2 seconds. On each update, I want to have the camera pan to the layer\'s feature\'s extent but I can\'t figure out where to set

Here's a stripped down version of my code that reloads a KML file every 2 seconds. On each update, I want to have the camera pan to the layer's feature's extent but I can't figure out where to set the callback.

//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);

the problem is that this line: map.panTo(trackLayer.getDataExtent().getCenterLonLat()); is being cal开发者_JAVA百科led before the request is finished.

Here's the docs:

  • 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


Nvm I got it. I feel stupid for not figuring this out faster.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号