22

I'm using OpenLayers to display a map in a web page. I am using tiles from CloudMade, but the same issues occur with the Mapnik tiles from OpenStreetMap.

I'm trying to restrict the map so that the user cannot zoom all the way out to world view -- I want them to stay at roughly a city level zoom at minimum.

I have tried using the minZoomLevel / maxZoomLevel / numZoomLevels properties; only the maxZoomLevel and numZoomLevels properties seem to work, whereas the minZoomLevel property seems to be completely ignored.

Is there another way to accomplish this?

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202

9 Answers9

11

You could override the isValidZoomLevel function. Something like this (not tested):

OpenLayers.Map.isValidZoomLevel = function(zoomLevel) {
   return ( (zoomLevel != null) &&
      (zoomLevel >= 2) && // set min level here, could read from property
      (zoomLevel < this.getNumZoomLevels()) );
}

EDIT

Or you would need to override the available resolutions, see the example here: http://dev.openlayers.org/examples/zoomLevels.html

pstanton
  • 35,033
  • 24
  • 126
  • 168
Jonathan
  • 5,953
  • 1
  • 26
  • 35
  • 1
    That isn't going to adjust the zoom bar though. – David Pfeffer Nov 22 '10 at 19:36
  • David, sorry ignored the controls. I've added more the my answer - see Edit. Essentially you'd need to define the resolutions you want to allow. – Jonathan Nov 23 '10 at 03:39
  • Although this answer doesn't work for the controls, it did fix my problem in that I wanted to simply limit the user from zooming past a certain level (via the scroll wheel or zoomToExtent). Thanks! – SoWeLie Aug 07 '12 at 17:36
  • For those of us like me who are still stuck in maintaining legacy OL2 code this is the cleanest way to add zoom restrictions. Hacky, but cleanest as with many other things with OL2 :) – Shaunak Dec 18 '15 at 02:54
9

minZoomLevel is not supported for XYZ layers, of which OSM is a subclass.

See the following tickets for workarounds:

geographika
  • 6,458
  • 4
  • 38
  • 56
8

I found the simplest way to restrict the maxZoom levels for a XYZ layer was to override the getNumZoomLevels method:

map.getNumZoomLevels = function(){
        return 10;
        };

This pevents zooming beyond level 10 and also draws the zoomBar control correctly. This and a combination of the zoomOffset option should allow you to easily control min/max zoom without having to mess around with resolutions or subclasses.

Graham
  • 6,484
  • 2
  • 35
  • 39
  • Agreed - its nasty, but after a few futile hours messing around with resolutions I didn't really care.... – Graham Apr 24 '12 at 11:58
  • Same here. Strange thing there's not a more straightforward way to do this, but well. Thanks again anyway. – Countzero Apr 24 '12 at 14:10
2

Other answers here refer to OpenLayers versions prior to version 3.

With OpenLayers 3, you can use the maxZoom map option when initializing the map, as follows:

    var map = new ol.Map({
        target: 'mapcontainer-001',
        layers: layers,  // Layers need to be initialized previously
        view: new ol.View({
          center: ol.proj.transform([-5.12345, 42.12345], 'EPSG:4326', 'EPSG:3857'),
          zoom: 12,
          maxZoom: 19
        })
    });

More information can be found in OpenLayers documentation: http://openlayers.org/en/v3.0.0/doc/tutorials/concepts.html

jjmontes
  • 24,679
  • 4
  • 39
  • 51
1

Now i use this in the "View":

view: new ol.View({
    center: ol.proj.transform([-3.707524, 40.485644], 'EPSG:4326',EPSG:3857'),
    zoom: 15,
    maxZoom: 17,
    minZoom: 6
}),

And it works fine

0

I ended up with this solution as i could not figure out how to use zoomOffset in my setup.

        map.getNumZoomLevels = function(){
            return (options.maxzoom-options.minzoom+1);
        };

        map.isValidZoomLevel = function(zoomLevel) {
            var valid = ( (zoomLevel != null) &&
                (zoomLevel >= options.minzoom) &&
                (zoomLevel <= options.maxzoom) );
            if(!valid && map.getZoom() == 0){
                map.zoomTo(options.maxzoom - (options.maxzoom - options.minzoom)/2);

            }
            return valid;
        }
netbrain
  • 9,194
  • 6
  • 42
  • 68
0

I tried something like this and it works for me:

map.newMoveTo = map.moveTo;
map.moveTo = function(lonlat,zoom,options){
    return(zoom>=maxZoom && zoom<=minZoom)?map.newMoveTo(lonlat,zoom,options):false;
};
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
Akna
  • 139
  • 2
  • 6
0

The best answer I found (including an example) was at OpenLayers - limit the number of zoom levels. I don't paste it here, please see the instructions over there.

nachtigall
  • 2,447
  • 2
  • 27
  • 35
0

Defining resolutions doesn't solve the problem (even in v2.1) - I didn't find any pernament fix for this, so I made my own zoombar in JS - that's what I would recommend to anyone who encounters issues with zoombar using custom tiles.

biphobe
  • 4,525
  • 3
  • 35
  • 46