you can do this with some programmatic way as registering map with zoomend
:
eventListeners
{Object} If set as an option at construction, the
eventListeners object will be registered with .
EVENT_TYPES
{Array(String)} Supported application event types. Register a
listener for a particular event with the following syntax:
map.events.register(type, obj, listener);
Listeners will be called with a reference to an event object. The
properties of this event depends on exactly what happened.
Solution Code:
var layer1 = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0",
{layers: 'basic'}, {'displayInLayerSwitcher':false} );
var layer2 = new OpenLayers.Layer.WMS( "NASA Global Mosaic",
"http://t1.hypercube.telascience.org/cgi-bin/landsat7",
{layers: "landsat7"}, {'isBaseLayer': false});
var layer3 = new OpenLayers.Layer.WMS( "DM Solutions Demo",
"http://www2.dmsolutions.ca/cgi-bin/mswms_gmap",
{layers: "bathymetry,land_fn,park,drain_fn,drainage," +
"prov_bound,fedlimit,rail,road,popplace",
transparent: "true", format: "image/png" });
layer2.setVisibility(false);
layer3.setVisibility(false);
map.addLayers([layer1, layer2, layer3]);
map.events.register('zoomend', this, function (event) {
var zLevel = map.getZoom();
if( zLevel == 13 || zLevel == 14 || zLevel == 15 )
{
layer2.setVisibility(true);
}
if( zLevel == 16 || zLevel == 17 || zLevel == 18)
{
layer2.setVisibility(false);
layer3.setVisibility(true);
}
});
i hope it helps you...
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...
Best Answer
In order to do this without modifying the actual source code for openlayers (which you won't be doing if you're using a minified version, which by default you probably are), look at this answer.
However, I had to change it slightly for my code, which is using OpenLayers 2.11
The only difference between this code and the one in the answer linked is that instead of
onButtonClick
, we're usingbuttonDown
, and rather thanevt.buttonElement
, we're usingevt.currentTarget
EDIT: Internet Explorer has no
currentTarget
property in their event handling. Luckily, we can usethis
instead. I have modified the code above accordingly. For an explanation of this, please see this article. A relevant stackoverflow question is here.