[OpenLayers-Users] Re: How to apply a Search Function
Phil Scadden
p.scadden at gns.cri.nz
Tue Feb 7 15:21:53 EST 2012
> *Thank you so much Mr.Phil
>
> but would you please explane your idea much more ?
> coz I'm new at field and need some details
Well be prepared for a fair bit of work and learning then.
Assume from application logic that you have the text you want to search
for and you know which layer you are searching.
First you create a filter object for the search eg. a wildcard like on
"myfieldname"
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.LIKE,
matchCase:false,
property: "myfieldname",
value: "*" + searchText + "*"
});
You could use tools though to create polygon or dwithin filters instead.
The logic doesnt depend on filter type.
To do a WFS search, then you need a wfs_protocol object for the layer
you are going to search.
eg
var wfsProtocol = new OpenLayers.Protocol.WFS.v1_1_0({
url: mywfsURL,
geometryName: "SHAPE",
featurePrefix: mywfsFeaturePrefix,
featureType: mywfsFeatureType,
srsName: myprojname // eg "EPSG:900913"
});
URL, prefix and featuretype depend on your WFS server and layer. Ditto
the name of geometry field
To do a search, with
wfsprotocol.read ({
filter:filter,
callback: processTheQuery,
scope: strategy
});
strategy is whatever you want for the app. eg
var strategy = new OpenLayers.Strategy.Fixed();
You might want a layer to display the selected features. eg.
selectedLayer = new OpenLayers.Layer.Vector("Selected Layer", {
displayOutsideMaxExtent: true,
displayInLayerSwitcher: false
});
You then write the callback, eg
function processTheQuery(request) {
// the first bit sets the value of sExt to be the a boundingbox on the
features returned.
// Depending on application logic, this could happen in other places in
the code.
if (request.data && request.data.bbox) {
var b = request.data.bbox;
sExt = new OpenLayers.Bounds(b[0],b[1],b[2],b[3]);
}else {
var fts = request.features;
if (fts.length>0) {
sExt = fts[0].geometry.getBounds().clone();
for(var i=1;i<fts.length;i++) {
sExt.extend(fts[i].geometry.getBounds());
}
}
};
selectedLayer.destroyFeatures(); // get rid of values from previous
call
// any other process of the returned features goes here eg display
attributes in a popup
selectedLayer.addFeatures(request.features);
map.zoomToExtent(sExt);
}
Notice: This email and any attachments are confidential. If received in error please destroy and immediately notify us. Do not copy or disclose the contents.
More information about the Users
mailing list