[OpenLayers-Users] Map load event?

Greg Allensworth gregor at greeninfo.org
Mon Apr 23 11:35:54 EDT 2012


> On Wed, Apr 18, 2012 at 3:23 AM, Tobias Reinicke <ramotswa at gmail.com
> I might have missed where this has been written, but is there not a
> "loadend" event for the entire map (rather than on a layer basis?). If
> not, what is the best way to check that all layers on a map have
> finished loading? I know there was some work done on the Loading Panel
> add-in, but as far as I can tell this iterates through all layers and
> registers loadend events on them.

That sounds about right. The Map itself is already loaded, it's "all 
layers" that you're interested in knowing, so you'll have to approach 
from that angle. I'd go with a counter and a self-canceling "layer 
finished loading" handler. It would look kinda like this:


var LAYERS_LOADING = 0;

layer.events.register('loadstart', layer, function () {
    LAYERS_LOADING++;
    document.getElementById('loading').style.display = 'block';
});

var cancelme = layer.events.register('loadend', layer, function () {
    LAYERS_LOADING--;

    if (LAYERS_LOADING == 0) {
       document.getElementById('loading').style.display = 'none';
       layer.events.unregister(cancelme);
    }

});



You should be able to stick that whole thing inside your map startup 
function, won't even pollute your global namespace.

In fact, if you put that whole shebang inside a nice OpenLayers.Control 
wrapper, and put that stuff inside the "if" inside a function instead, 
you'd probably have a reusable Control. Or maybe this 10-line block 
minus the "if" stuff would be feasible to add to OpenLayers.Map so it 
really does raise a map.doneloading event ...

-- 
Greg Allensworth, Web GIS Developer
BS  A+  Network+  Security+  Linux+  Server+
GreenInfo Network - Information and Mapping in the Public Interest
564 Market Street, Suite 510  San Francisco CA 94104
PH: 415-979-0343 x302  FX: 415-979-0371    email: gregor at greeninfo.org
Web: www.GreenInfo.org     www.MapsPortal.org

Subscribe to MapLines, our e-newsletter, at www.GreenInfo.org


More information about the Users mailing list