Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


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});
share|improve this question
I've just been told that the loadend event doesn't work in all browsers, which would torpedo this approach, so, before I waste anyone's time, does anyone know if this is true? – bethesdaboys Nov 10 '11 at 16:37

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

You should call this that:{"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.

share|improve this answer
Cool! So then this is what I'm trying so far (see code added to original question, I can't see how to add code to a comment) but I don't know whether it's correct and since the layer is created in the same statement that the KML layer is added, I don't know how to register the event on the layer before the KML is added. Is there a way to first construct the layer, then add the listener, then load the KML? thanks again! – bethesdaboys Nov 10 '11 at 16:08

Decided to bail on this after finding too many people having trouble with loadend working. I'm going to simply issue another server call rather than trying to figure out when my kml layer is finished loading. Thanks.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.