[GIS] Zoom after apply CQL_FILTER in OpenLayers

cql-filterjavascriptopenlayers-2

I apply a filter to one layer and works nice. I want to maximize the bounds where I applied the filter, like this:

// mapbounds = array(minx,miny,maxx,maxy)

activeLayer = new OpenLayers.Layer.WMS(
      layername, layerurl,
      {
         width: map.size.w,
         srs: map.projection,
         layers: layername,
         height: map.size.h,
         styles: '',
         format: 'image/png',
         transparent: false
      }
      );

 // for example: conditions = 'igh > 5000 AND BBOX('the_geom,1,2,3,4)'    
 activeLayer.mergeNewParams({
         'cql_filter': conditions
      });

bounds = new OpenLayers.Bounds(mapbounds[0],mapbounds[1],mapbounds[2],mapbounds[3]);

map.zoomToExtent(bounds);

The filter works fine, but the zoom is restored too quickly after apply it. It's applied, but the layer restore to the initial view instantly.

What am I doing wrong?

Best Answer

you can use tileloaded or loadend event for firing zoomToExtent method.

activeLayer.events.register("tileloaded", activeLayer, function() {
map.zoomToExtent(bounds);
});

activeLayer.events.register("loadend", activeLayer, function() {
map.zoomToExtent(bounds);
});

OR

you can use setTimeout function to make it wait for 5 minute...

function sleep()
{
  setTimeout(wake, 5000); //wait five seconds then fire the wakeup function
}

function wakeup()
{
   map.zoomToExtent(bounds); //  now zoom to extent
}

i hope it helps you...