I'm trying to zoom to the extent of several vector layers data.
I've tried to create a new OpenLayers.Bounds bounds and to add a 'loadend' for each OpenLayers.Layer.Vector to increase bounds, but then I don't know where to put map.zoomToExtent(bounds)... is there a way to run this after all the layers have been loaded?
I've tried https://gis.stackexchange.com/a/6297, but it did not work, as the vector layers were not created yet...
I've also tried https://gis.stackexchange.com/a/41588/16356, but it did not work either, as I have several layers.
EDIT: Here is how I fixed the issue, thanks to Vadim.
bounds = new OpenLayers.Bounds();
map = new OpenLayers.Map('map');
LOADED_LAYERS = 0;
layers[1] = new OpenLayers.Layer.Vector("layer1", {
eventListeners: {
'loadend': function (evt) {
LOADED_LAYERS++;
bounds.extend(layers[1].getDataExtent());
if (LOADED_LAYERS == 3) {
map.zoomToExtent(bounds);
};
}
}
});
layers[2] = ...;
layers[3] = ...;
map.addLayers(layers);
map.zoomIn();