5

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});
asked Nov 10, 2011 at 0:55
1
  • 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? Commented Nov 10, 2011 at 16:37

2 Answers 2

9

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.

answered Nov 10, 2011 at 7:27
1
  • 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! Commented Nov 10, 2011 at 16:08
0

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.

answered Nov 10, 2011 at 17:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.