Javascript OpenLayers 中的最小/最大缩放级别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4240610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:32:28  来源:igfitidea点击:

Min/max zoom level in OpenLayers

javascriptzoomopenlayersopenstreetmapcloudmade

提问by David Pfeffer

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.

我正在使用 OpenLayers 在网页中显示地图。我使用的是来自 CloudMade 的图块,但是来自 OpenStreetMap 的 Mapnik 图块也出现了同样的问题。

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/ numZoomLevelsproperties; only the maxZoomLeveland numZoomLevelsproperties seem to work, whereas the minZoomLevelproperty seems to be completely ignored.

我已经使用试图minZoomLevel/ maxZoomLevel/numZoomLevels性能; 只有maxZoomLevelnumZoomLevels属性似乎有效,而该minZoomLevel属性似乎完全被忽略。

Is there another way to accomplish this?

有没有另一种方法来实现这一点?

采纳答案by geographika

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

XYZ 层不支持 minZoomLevel,其中 OSM 是其子类。

See the following tickets for workarounds:

有关变通方法,请参阅以下票证:

回答by Jonathan

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

您可以覆盖该isValidZoomLevel功能。像这样的东西(未测试):

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

或者您需要覆盖可用的分辨率,请参见此处的示例:http: //dev.openlayers.org/examples/zoomLevels.html

回答by Graham

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

我发现限制 XYZ 层的 maxZoom 级别的最简单方法是覆盖 getNumZoomLevels 方法:

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

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

这可以防止缩放超过 10 级,并且还可以正确绘制 zoomBar 控件。这和zoomOffset选项的组合应该允许您轻松控制最小/最大缩放,而不必弄乱分辨率或子类。

回答by jjmontes

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

此处的其他答案是指版本 3 之前的 OpenLayers 版本。

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

使用 OpenLayers 3,您可以maxZoom在初始化地图时使用地图选项,如下所示:

    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

更多信息可以在 OpenLayers 文档中找到:http: //openlayers.org/en/v3.0.0/doc/tutorials/concepts.html

回答by Fernando Sanchiz

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

它工作正常

回答by netbrain

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

我最终得到了这个解决方案,因为我无法弄清楚如何在我的设置中使用 zoomOffset。

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

回答by 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;
};

回答by nachtigall

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.

我找到的最佳答案(包括一个示例)是在OpenLayers - limit the number of zoom levels。我这里不贴,请看那边的说明。

回答by biphobe

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.

定义分辨率并不能解决问题(即使在 v2.1 中) - 我没有找到任何永久修复,所以我在 JS 中制作了我自己的缩放栏 - 这就是我向任何遇到使用自定义缩放栏问题的人推荐的瓷砖。