OpenLayers - setting max and min zoom levels?

So I’m using OpenLayers with OSM for a slippy map.
Is there any way to restrict the min zoom level, ie how far you can zoom out?
(It’s not really a problem to have the smaller zooms, it just my map is supposed to be fairly limited area, so its pointless zooming out to the whole world, plus it makes the zoom bar much bigger than it needs to be).

I found this page here:
It says you can use minZoomLevel and maxZoomLevel to specify this, but this doesn’t seem to work for me - it doesn’t affect the map at all.
Setting numZoomLevels does work, but it starts the zoom levels from 0. eg I set numZoomLevels to 10, it gives me levels from 0 to 9, whereas I would rather have something like levels 10-19.

I’ve got a test page here, so you can look at the code I’ve tried:
Any ideas, or links to working examples?

I think you have a problem with various things overriding eachother. In those SettingZoomLevels docs, scroll down to where it talks about “The precedence rules are then as following”.

‘numZoomLevels’ takes precedence, and in OpenStreetMap.js (Which has the definition of OpenLayers.Layer.OSM.Mapnik you’re inheriting from) we actually have a numZoomLevels: 19

Try this.

Firstly remove any reference to minZoomLevel, minZoomLevel, or numZoomLevels in your ‘map’ definition

Then in your layer definitons do { numZoomLevels: null, minZoomLevel: 8, maxZoomLevel: 16 }

With a bit of luck the null will override the numZoomLevels: 19 I just tried it and it seems to work. Maybe someone else can suggest a more elegant solution

Can I take this a bit further? I confess that zoom levels - like much of OpenLayers - completely baffle me.

I want to limit my map to zoom levels 12-18 (as defined on the main OSM homepage). For this experiment start with a very standard OpenLayers map with a Mapnik layer. I have tried the following approaches without success:

  • Set numZoomLevels to null, minZoomLevel to 12 and maxZoomLevel to 18 on the map only (no effect)

  • Set numZoomLevels to null, minZoomLevel to 12 and maxZoomLevel to 18 on the OpenLayers.Layer.OSM.Mapnik layer only (smaller range of zoom levels, but starts at 0 and only goes up to level 15)

  • Set numZoomLevels to null, minZoomLevel to 12 and maxZoomLevel to 18 on the OpenLayers.Layer.OSM.Mapnik layer, and all three values to null on map (whole map fails to display)

  • Set numZoomLevels to null, minZoomLevel to 12 and maxZoomLevel to 18 on the map, and any/all three values to null the OpenLayers.Layer.OSM.Mapnik layer (whole map fails to display)

Can anybody point me in the right direction?

I’ve tried various things like Tom Chance did too but no success and Harry Wood’s suggestion changes nothing for me. The only thing I have been successful in is limiting the maximum (depth) amount of zoom levels. Here’s my JavaScript code:

function init() {   
    // Create simple OpenLayers map 
    var mapOptions = {
                new OpenLayers.Control.Navigation(),
                new OpenLayers.Control.PanZoomBar(),
                new OpenLayers.Control.Attribution()
            projection: new OpenLayers.Projection("EPSG:900913"),
            displayProjection: new OpenLayers.Projection("EPSG:4326")
    var map = new OpenLayers.Map ("mapdiv", mapOptions);

    // Add the Mapnik layer from OpenStreetMap and limit the amount of zoomlevels to 6
    var layerOptions = {numZoomLevels: 6};

    var layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik", layerOptions);

    // Add the layer to the OpenLayers map
    // Set the center point of the map (without it the tiles are not loaded somehow)
    map.setCenter(new OpenLayers.LonLat(0, 0));

Live example here.

Anyone out there that can modify this code to limit the zoomlevels to e.g. from 5 to 10?

Not your code but see here. As far as I know OL doesn’t support minZoom, therefore I’ve changed several controls to add this functionality. IMO it would be best if these controls were integrated into OL yet this has to be done by sombody else.

Ohh, I just see that it’s not correctly working anymore since I’ve change some other things. Just wait some time I’ll fix it in the next few days.



I figured out a hack that seems to work.'zoomend', this, function (event) {
	var x = map.getZoom();
	if( x < 15)

Put that in your init function. The zoomend event fires at the end of a zoom, so you simply check to see whether the new value is something you want, and if not you just set it to a valid value.

It’s pretty ugly, and I haven’t tested it thoroughly, so its a use at your own peril sort of thing.

If you use OpenLayers 2.11 which is currently at RC1 stage I’ve found some success with the following syntax

var OSMLayer = new OpenLayers.Layer.OSM(“OSM”,“”,{isBaseLayer:true,displayInLayerSwitcher:true,zoomOffset:11,resolutions: [76.4370282714844,38.2185141357422,19.1092570678711,9.55462853393555,4.77731426696777,2.38865713348389,1.19432856674194]});

as seen at the rough map at Isle of Wight Paths web site

Note this code is a bit rough, and I’m not saying this is the best way to do it, but it works for me! Also as it is based on RC1 there may be some problems I’m not aware of.

Thanks for this

This solution looks quite good, but it’s a little problem for me. How should I calculate a parameter resolution? I calculated Xmax-Xmin/256 and it’s wrong.