Snippets/WFSFilter

Version 3 (modified by tschaub, 5 years ago)

--

Untested code from Tim that I didn't want to lose to rafb.net's 24 hour limit: this might help if you care about wfs filters, and is probably a better solution than using a Layer.WFS to solve the problem.

var layer = new OpenLayers.Layer.Vector("My Features");
var format = new OpenLayers.Format.GML({extractAttributes: true});

function callback(request) {
    // maybe you want to destroy all features from your layer first
    layer.destroyFeatures();
    // parse the response
    var features = format.read(request.responseXML | request.responseText);
    // add the new features to your layer
    layer.addFeatures(features);
}

var base = "http://host/path/to/wfs";

// when the user gives you some input, create a filter and other params
var params = {
    "SERVICE": "WFS",
    "VERSION": "1.0.0",
    "REQUEST": "GetFeature",
    "FILTER": "foo"
};

// now issue a request and set your callback 
var url = base + "?" + OpenLayers.Util.getParameterString(params);
var req = OpenLayers.loadURL(url, null, null, callback);
// when the request comes in, your callback will be called