I am using Openlayers,Geoserver & Postgis.... My postgis tables are having columns as id, name, POINT/POLYGON/LINE. Each table corresponds to a layer that I am showing in layerswitcher in OL. I am getting these layers via WMS from geoserver.

I want to implement search functionality so that whenever a user gives a string(name value of the table) in search box, he should be zoomed to that particular feature on the map.

Can anyone guide me how to achieve this functionality??? Any help is highly appreciated

The idea I got is:

Step 1: User has to check only one layer(the layer in which he want to search) in layerswitcher before searching . But how to get which layer has been selected??

Step 2: After knowing the selected layer , Using php search that corresponding table for the search string in names column , if it is present get the latitude and longitude values. But how to get these values and what if they are polygons/lines???

Step 3: how to send these latitude longitude values to JS so that I can zoom to that point???

I have completed 2 1/2 steps. I am using st_box2d and getting the bounds of the feature as "BOX(minx miny,maxx maxy)"...... where bounds contain BOX(minx miny,maxx maxy)

the code below is the ajax function for fetching bounds from db. Now how to use bounds variable to zoom to that feature.

 <?-- code for displaying map -->
 function searchfunction(form)
            httpObject = getHTTPObject();
            var searchvalue=form.searchvalue.value;
            if (httpObject != null) 
                httpObject.onreadystatechange = function()
                    if(httpObject.readyState == 4)
  • 2
    How fancy do want to get? Do you want to do fuzzy matching to catch misspellings? Full text searching? Or can you simply rely on the user to enter a (mostly) complete correct name?
    – Sean
    Commented Jun 17, 2011 at 12:38
  • simply rely on the user to enter a (mostly) complete correct name Commented Jun 17, 2011 at 12:51
  • I just do this webgis youtube.com/watch?feature=player_embedded&v=gci2HkSpdz4. But do not write the search function for the object is the land .. Please help me! Please code search function in webgis use tool openlayer (geoserver) or asp.net C # ... Thanks!!
    – user16236
    Commented Mar 16, 2013 at 16:34
  • Hi! Welcome to the site. It sounds like you have questions that were not addressed by the answers to this one. Why not ask a new question? Commented Mar 16, 2013 at 17:15

2 Answers 2


You can pass query terms in the URL to an SQL View in GeoServer 2.1 and above. Create the view with an ilike clause and you should get the basic functionality. Have OpenLayers fetch the view with the supplied name as a parameter. Then, tell OpenLayers zoom to the extent of the result.

If you don't have GeoServer 2.1:

  1. Get the name of the feature with Javascript/OpenLayers,
  2. pass the name asynchronously to a php script on the server,
  3. the php script will query the database directly for the st_envelope or st_box2d of the feature with that name (select st_envelope(the_geom) from table where name ilike passed_parameter),
  4. the php script returns the extent/bounding box of the feature which is used in Javascript/OpenLayers to zoom the map.

You'll have to parse the st_envelope result into something that OpenLayers understands. You also might want to add a little buffer to the zoom extent. You can add this manually or use st_expand instead of st_envelope.

  • I am using geoserver-2.0.3 and now it wud be difficult to me to switch to 2.1.x.... Commented Jun 17, 2011 at 14:15
  • my map projection is 900913 and the features I am storing in db are projection 4326. Now using "select st_box2d(the_geom) from table where name ilike passed_parameter" i am getting bounds in 4326 projection. How to convert them into 900913??? Commented Jun 18, 2011 at 17:39
  • st_transform(st_box2d(the_geom),900913) check the postgis docs to make sure that's correct syntax.
    – Sean
    Commented Jun 18, 2011 at 22:59
  • Yeah its working...... But how to zoom to specific bounds after getting the bounds. I am really stuck up there Commented Jun 19, 2011 at 12:22
  • Read up on OpenLayers.Map.zoomToExtent in the OpenLayers docs.
    – Sean
    Commented Jun 20, 2011 at 3:29

Are you looking for something like http://ian01.geog.psu.edu/geoserver/www/search/gazetteer.html , if so then you can follow the instructions at http://ian01.geog.psu.edu/geoserver_docs/apps/gaz/index.html to create your own version.

  • @iant, But I am not using GeoExt and wanted to do everything by just OL/Geoserver/postgis. Commented Jun 17, 2011 at 14:31
  • the requests and actual zoom to point code will be identical. All you have to do is write the code that GeoExt provides for you.
    – Ian Turton
    Commented Jun 17, 2011 at 14:32
  • OK, I shall try this but what if the layer is a polygon or line layer Commented Jun 17, 2011 at 14:41
  • 1
    – Ian Turton
    Commented Jun 17, 2011 at 14:45
  • 1
    @iant those links are both failing for me - are those examples available elsewhere? thanks Commented Mar 6, 2017 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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