I would like to have only 3 levels of zoom available (from level 6 to level 8).

demolayer2 = new OpenLayers.Layer.WMS(
{layers: 'def...', transparent:"true", format: 'image/png', numZoomLevels: 3, minZoomLevel: 6 },{isBaseLayer:false},

{ tileSize: new OpenLayers.Size(256,256)});


 map.addControl(new OpenLayers.Control.LayerSwitcher({'div':OpenLayers.Util.getElement('layerswitcher')}));

map.zoomToExtent(new OpenLayers.Bounds(-4.601615515076983,39.8769407866263,-3.0527184873294764,41.16710732525929));

One way of doing that (don't know if it's the only way) is by passing an array of available resolutions to the map constructor via the options parameter. Something like...

var map = new OpenLayers.Map('map', {
    resolutions: [0.02197265625, 0.0439453125, 0.17578125]

A way to get the resolutions you are interested in could be:

1) zoom your map to a desired zoom level (I did it via console by running map.zoomTo(x) where x is the zoom level I was interested in)

2) via the console log the map.resolution and take note of it

3) repeat for all the zoom levels you would like to include

You can find more in-depth information here.


In OlpenLayers 3 you can just initialize your view with the desired zoom constraints:

var map = new ol.Map({
    target: 'map',
    // ...
    view: new ol.View({
        maxZoom: 8,
        minZoom: 6,
        zoom: 7
  • I changed the question title to make it specific to OpenLayers 2.x, as the API is very different from version 3. You might consider creating a new question for version 3. – mettjus Oct 21 '15 at 10:26
  • @mettjus "I changed the question title to make it specific to OpenLayers 2.x" - I do not think that is a valid edit. – vbence Oct 21 '15 at 15:38
  • I asked for moderator intervention. We'll see what they suggest, if they do. Absolutely not meaning to invalidate your answer just trying to make the question more helpful for new comers. – mettjus Oct 22 '15 at 13:00
  • @mettjus I understand. I'm not sure either which is the best solution. I made a meta discussion, feel free to add your 2 cents: meta.stackexchange.com/questions/268244/… – vbence Oct 22 '15 at 15:56

just add the following code to your script:

map.isValidZoomLevel = function(zoomLevel) {
        return ((zoomLevel != null) && (zoomLevel >= 6) && (zoomlevel <= 8));
  • 1
    I just love when you google and google and try things out, and after 20 minutes you find the right answer like this and it's sweet. Made my morning. – Johan Danforth Nov 8 '16 at 8:42

This should also be possible by setting numZoomLevels and maxResolution (maybe easier than individually finding all resolutions):

var map = new OpenLayers.Map('map', {
    numZoomLevels: 15, 
    maxResolution: 39135.758475

Note: resolution for a given zoom level is previous zoom level's resolution divided by 2.

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.