0

I'm using openlayers 2.12 and geoserver 2.1.3.

I created a search functionality for a vector layer that contains points/lines/polygons. I used CQL that dynamically gets data from a form.

When I use map.zoomToExtent(pins.getDataExtent()); does not work at all, instead it zooms 100% on an irrelevant place of the map. Never zooms to contains just the newly created features.

Map also contains other vector layers with no search functionality and a Google maps layer. All projections are EPSG 900913.

Check the following code. Form's "name" is a textfield, "era" is a dropdown and "borough" is a dropdown containing string that will turn to Bounds , so map will zoom to a borough.

Search for : era OR name OR era+name with zoomToExtent (getDataExtend) on = zooms 100% at irrelevant place. I want this to zoom at new features.

Search for : era OR name OR era+name with zoomToExtent (getDataExtend) off = does not zoom, as it should be

Search for : era+borough OR name+borough OR era+name+borough with zoomToExtent (getDataExtend) on = zooms 100% at irrelevant place. I want this to zoom at new features.

Search for : era+borough OR name+borough OR era+name+borough with zoomToExtent (getDataExtend) off = zoom,at the borough as it should be.

The code...

//first, clear the layer
pins.removeFeatures(pins.features);
pins.refresh({force:true});

//split the chosen borough, to create bounds, so the map can zoom there
var binfo=document.getElementById("boroughselect").value;
if(binfo!=" "){
var split_binfo = binfo.split(",");
var bidchoosen=split_binfo[0];
var zoomTo=new OpenLayers.Bounds(split_binfo[1], split_binfo[2], split_binfo[3], split_binfo[4]).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
map.zoomToExtent(zoomTo);}

//create a filter using CQL
var filter_list = []

if (document.getElementById("eraselect").value != " ") {
    var filter_b = new OpenLayers.Filter.Comparison({
        type: OpenLayers.Filter.Comparison.EQUAL_TO,
        property: 'p_era', 
        value: document.getElementById("eraselect").value
    });
    filter_list.push(filter_b);}

if (document.getElementById("nm").value != "") {
    var filter_c = new OpenLayers.Filter.Comparison({
        type: OpenLayers.Filter.Comparison.LIKE,
        property: 'p_name', 
        value: document.getElementById("nm").value
    });
    filter_list.push(filter_c);}

//create the final filter,combining the above,  to search
var filter_final = new OpenLayers.Filter.Logical({
    type: OpenLayers.Filter.Logical.AND,
    filters: filter_list});

//pass the filter to the layer
var prot =  new OpenLayers.Protocol.WFS({
    url:  "http://localhost/geoserver/wfs",
    featureType: "pins",
    featureNS: "http://www.cultmap.gr",
    defaultFilter: filter_final});

var _CallBack = function(resp) {
    pins.addFeatures(resp.features)

var cb = pins.features.length;
if (cb == 0){
    document.getElementById("formMsgG").innerHTML = No results, please try again.";
            }};

var response = prot.read({callback: _CallBack});

pins.refresh({force:true});
map.zoomToExtent(pins.getDataExtent());

Thank you for your time

slevin

1

The same issue has been well answered in this question. But here is a quick answer, the reason is that by the time you call pins.getDataExtent() the wfs protocol has not finished reading all the features, so the _CallBack has not been executed. In your case you may zoom to layer's data extent after add features to the layer, i.e. in _CallBack function after this line:

pins.addFeatures(resp.features)
  • First of all I am happy that someone ansered. I thought my question is so big that nobody will anser. Second, thanks alot sir, it worked right away, by calling pins.getDataExtend() after pins.addFeatures(resp.features) in the _Callback. Thanks for helping and for your time – slevin Aug 29 '13 at 14:06

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.