[GIS] Zoom to country with geoserver and openlayers

featuresgeoserveropenlayers-2

I would like to preposition an OpenLayers map on a country.
This is, when my page loads, the map should be positioned on the country with an appropriate zoom level. This should work for a (large) list of countries.

I have a GeoServer with a shapefile layer containing country polygons. The polygons have ISO country code attributes. Another layer shows a satellite background image.
When generating the country page on which the map is shown, I only know the country name (or ISO code), but not its coordinates and polygon details. (GeoServer and webserver are different machines)

My knowledge of OpenLayers and GeoServer is limited but if I understand correctly OpenLayers requests a certain map position and zoom level from GeoServer. I would thus need to retrieve position and zoom level from GeoServer in a first step, and then request this part of the map in a second one. This is if there is not a way to tell OpenLayers to zoom in on a feature I can identify by its attribute.

Any code example would be very much appreciated, but conceptual clarification would be equally valuable.

Best Answer

you should try to get your shape extent via using CQL Filter. there is so good example in openlayers for filtering a feature here...

you can add your cql filter to iso codes..

wms.mergeNewParams({
         'cql_filter': conditions // 'iso_code = your ISO country code'
      });

then get vector layer extent (i write this code to the example which i give it above)

var dataExtent = states.getDataExtent();
map.zoomToExtent(dataExtent);

i hope it helps you...