Min/max zoom level in OpenLayers

泄露秘密 提交于 2019-11-28 09:02:49

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

See the following tickets for workarounds:

Jonathan

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

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.

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

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

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.

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;
        }
Akna

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;
};

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!