I created a layer that contains points and I added it to a map as WMS layer in OpenLayers. I did a CQL filter and it works but I'd like to zoom to the selected point when I worked with getextent or getdataextent it doesn't zoom to the right place. It missed with a few Km. Here is my the code of filter:

function filterNom(){
            var filterType ="cql";
            var filter = document.getElementById('batima').value;
            // by default, reset all filters
            var filterParams = {cql_filter: null};
            if (OpenLayers.String.trim(filter) != "") {
                if (filterType == "cql") 
                    filterParams["cql_filter"] = "batiment='"+filter+"'";

            // merge the new filter definitions
            var bounds =  new OpenLayers.Bounds
            bounds = wfs_layer.getExtent()
            var center = map.getCenter();
            center.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));

and here is the code to add the WMS layer:

  wfs_layer = new OpenLayers.Layer.WMS("Patrimoines",
        "http:// localhost/geoserver/cite/wms", 
        {layers: 'cite:patrimoine' , transparent: true },
        {isBaseLayer: false, opacity: 1, singleTile: true, visibility: true,
         projection: 'EPSG:900913'});

Your wms layer is a image, so the zooming to it will not work. I set up a vector layer with a protocol and filter and then am able to zoom to the particular feature. I did some cutting and pasting but I think I got the important parts. Let me know if this is sufficient

filter = new OpenLayers.Filter.Comparison({
    type: OpenLayers.Filter.Comparison.EQUAL_TO,
    property: "field name",
    value: 123

protocol = {
    version: "1.1.0",
    srsName: "EPSG:900913",
    url: your url,
    featurePrefix: "prefix",
    featureNS: namespace,
    featureType: featuretype,
    geometryName: "geom"

vector_LAYER = new OpenLayers.Layer.Vector("vector", {
    strategies: [new OpenLayers.Strategy.Fixed()],
    styleMap: styleMap,
    filter: filter,
    protocol: protocol

    featureadded: function() {

Then somewhere down down in my code I set the filter and refresh the layer.

filter.value = filter;

thanks for the help I set up 2 strategies on my layer on of them it's for filter.

filterStrategy = new OpenLayers.Strategy.Filter();

wfs_layer = new OpenLayers.Layer.Vector("Editable Features", {
    strategies: [ new OpenLayers.Strategy.Fixed(),filterStrategy],
    //filter: filter,
    protocol: protocol

   featureadded: function() {

after I made a function that update filter

function updateFilter(proprety,value) {

   var filter = new OpenLayers.Filter.Comparison({
       type: OpenLayers.Filter.Comparison.LIKE,
       property: proprety,
       value: value


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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