7

I have my map running over GeoServer and OpenLayers, but I want to add a search box to find places in it. Well not specifically places but directions.

The map reads the layers directly over GeoServer (without PostGIS) but I'm a bit lost with the search thing.

How can I implement this feature and what tools are appropriate for it?

Now I will explain how the searching process must be:

1.- I have layers with streets, blocks and house numbers.

2.- the user will search by street name or house direction (house number and street name) and the map will show a list of concordances, if the user select one result on the list it will be zoomed to the destination. Very similar to an ArcGIS search tools.

Any help will be appreciated.

1 Answer 1

8

You can preatty easily run a search by combining WFS anc CQL like in the following example:

layerQueryRun: function() { var wfs = new OpenLayers.Protocol.HTTP({ url:WMSCONFIG.wfs_server_path+"?service=wfs&version=1.0&request=GetFeature&typename=" + youLayerTypeName, format: new OpenLayers.Format.GML.v3({}) }); // start spinner Maps.set("isLoading", YES); wfs.read({ params:{ "CQL_FILTER":"STREET = 'Godric Hollow' AND NUMBER=1" }, callback: this.didFetchWfsFeatures }); }, didFetchWfsFeatures : function(response, options) { try { var gml = new OpenLayers.Format.GML({extractAttributes: true}); response.features = gml.read(response.priv.responseXML); doSomethingWithFeatures(response); } catch(e) { alert("Error!"); } // stop spinner Maps.set("isLoading", NO); }, 

The CQL reference is available here.

Once you have loaded the features from the results (in doSomethingWithFeatures) you can render them to HTML and bind an event listener to each so that when a feature is clicked the application is notified and it can zoom the map to the feature bounds.

The features returned from the WFS call have both attributes and geometry. Please note that the geometry will be in the original layer's projection which could be different from what you are using in OpenLayers (I guess 900913?). In that case you can either:

  • use proj4js to handle the conversion for you in the browser

  • reproject the original data

3
  • Thank you for answer =), but i have a question..my map works with wms layers, your code is adaptable to this protocol or i should have to switch to wfs? Commented Dec 16, 2011 at 16:31
  • 2
    You can mix and match WFS and WMS anyway you want: WMS for massive rendering, WFS for single features and searches Commented Dec 16, 2011 at 19:38
  • this is helpful, but any working example in the implementation? Commented Aug 13, 2015 at 1:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.