[GIS] How to time function calls in OpenLayers

javascriptopenlayers-2

We have an existing application that has a function that wraps the OpenLayers.Layer.Vector constructor, which gets its layer info from a KML source over http. Once this function completes, I need to run another function which will read some of the layers data from the map object.

Normally in JavaScript I would of course use a callback when executing a remote AJAX call like this. But in this case — perhaps because I'm still pretty new to JavaScript — I don't know how to delay the execution of my function until the Vector constructor is done. There doesn't seem to be any ability to pass a callback to the OpenLayers.Layer.Vector constructor. Any suggestions? Thanks.

[Addendum]

How do I add the listener to the layer before the KML is loaded?

var newLay = new OpenLayers.Layer.Vector(name, {
    displayInLayerSwitcher: true,
    visibility: visible,
    styleMap: MapLayers.styleMaps.polygonMap,
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
        url: url,
        format: new OpenLayers.Format.KML({
            maxDepth: 2,
            extractAttributes: true,
            extractStyles: useStyles
        })
    })
});

function loadEndListener() {
  alert('loading ended);
}

var events = new OpenLayers.Events(newLay);
events.on({"loadend": loadEndListener});

Best Answer

You can use register event loadend on your layer. More on:

http://dev.openlayers.org/docs/files/OpenLayers/Events-js.html#OpenLayers.Events.on

You should call this that:

newLay.events.on({"loadend":loadEndListener});

I make it a long time ago, but probably that should work. On another site I found that should be tilesloaded listener, but I don't check it.

I'm sorry that so late, but I have a free.