I have added WMS layers using follow:

basemap = new OpenLayers.Layer.WMS( "Layer Name1",
"http://localhost:8080/geoserver/wms/workspacename", {layers: 'workspacename:layername'} );  
utility = new OpenLayers.Layer.WMS( "Layer Name2",
"http://localhost:8080/geoserver/wms/workspacename", {layers: 'workspacename:layername', transparent:"true"}, {isBaseLayer:false} );

Now I want to get zoom to my utility layer, How can I?

I have tried as following:


From your syntax, it looks like you are using OpenLayer 2.x

The OpenLayers.Layer.WMS does not implement the getDataExtent() method, so you cannot use it to zoom into the Layer Extent.

There is nothing inbuilt within OpenLayers that can get the extent of a WMS layer, and hence you'll have to roll your own.

In most cases, where you control the layer, you usually look up the extent, and hardcode it within the code.

In cases where you do not know the extent, for example if a WMS layer is dynamically created based on user provided URL, you'll have to get the Extent from the WMS server.

You can call the GetCapabilities request on the WMS endpoint. This gives you an XML response, which can be parsed to read the bbox of the appropriate Layer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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