I have WMS map, after applying CQL filter to layer how can I zoom map to see filter result?


Here is my code what I'm trying to do

var layerArray = map.layers;
var someLayer;

for (var i = 0; i < layerArray.length; i++) {
    if (layerArray[i].name == "myLayer"){
        someLayer = layerArray[i];

    "CQL_FILTER": celIDCondition /* (CID =1351 and LAC = 401) */

// instead of somelayer.maxExtend I want to extent map acording to CQL_FILTER result
  • it seems that you need to expand this question to give us a better idea about what you are trying to do. Maybe you could post the code you are using so far?
    – Ian Turton
    May 18, 2012 at 7:27

3 Answers 3


you can try:

var dataExtent = vectors.getDataExtent();




vectors is the layer which you add features from CQL_Filter.

you can try it here with firebug writing this to console:



var fixWMSLayerExtent = function(layer)
var readr = new Ext.data.XmlReader(
{ record: 'Layer'},
{name: 'name', mapping: 'Name'}
,{name: 'minx', mapping: 'LatLonBoundingBox > @minx'}
,{name: 'miny', mapping: 'LatLonBoundingBox > @miny'}
,{name: 'maxx', mapping: 'LatLonBoundingBox > @maxx'}
,{name: 'maxy', mapping: 'LatLonBoundingBox > @maxy'}

var myStore = new Ext.data.Store({
url : "/MapProxy?url=" + layer.url +'&request=GetCapabilities' ,
reader : readr
myStore.load({'layer' : layer, callback: function(r, options, success)
var rindex = this.find('name', options.layer.name, 0, true, true);
var rec = this.getAt(rindex);
layer.maxExtent = new OpenLayers.Bounds(rec.data.minx, rec.data.miny, rec.data.maxx, 
} });

the code is from Sergey Malgin...

i hope it helps you...

  • it takes me to the center of worlds map. when I alert dataExtend it gives undefined. my layer is wms layer not vector layer. Thanks
    – Lazonaden
    May 17, 2012 at 14:17
  • ok. so you directly use wms layer... there is a solution for this... i have updated my answer...
    – urcm
    May 17, 2012 at 14:21
  • seems to be good solution but I am not using Ext
    – Lazonaden
    May 18, 2012 at 7:08

if you just have a point then you can use something like:

     new OpenLayers.LonLat(longitude,latitude),

You can see a whole application which demonstrates this process at http://ian01.geog.psu.edu/geoserver_docs/apps/gaz/index.html


You can't do what you want directly I'm afraid. The bounds of a WMS layer are defined in the request URL so the requested image will always be the same as the OpenLayers.Map object it is in (otherwise there would be edges and uncoloured in bits in the map).

The only way you will be able to do this is to make a WFS request (so that you ask for the actual data instead of a picture of the data) and then calculate the bounds of the returned object and zoom to that or calculate the centroid of the object and set a zoomlevel and center the map on that point.

  • no I have something like this someLayer.mergeNewParams({ "CQL_FILTER": celIDCondition });
    – Lazonaden
    May 17, 2012 at 14:22

If i read your code (and comments) correctly, i assume that "someLayer" is a WMS-layer, is this correct?

If so, getting the maxExtent is a bit problematic, as wms does not support this. If your WMS server offers the layer as WFS in addition to WMS, you could do a WFS request to the server, using the same filter as your WMS request, and get the extent from the features returned from the WFS request.

I could try to sketch an outline for the code doing this, but would like to get a confirmation that your someLayer is indeed a WMS layer.

  • Yes someLayer is WMS layer
    – Lazonaden
    May 18, 2012 at 9:12

Your Answer

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

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