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

发布于 2024-10-03 11:38:51 字数 375 浏览 1 评论 0原文

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

我试图限制地图,以便用户无法一直缩放到世界视图 - 我希望它们至少保持在大约城市级别的缩放。

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

还有其他方法可以实现此目的吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

橙味迷妹 2024-10-10 11:38:51

您可以重写 isValidZoomLevel 函数。像这样的东西(未经测试):

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

编辑

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

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

梨涡 2024-10-10 11:38:51

XYZ 图层不支持 minZoomLevel,OSM 是该图层的子类。

请参阅以下票证了解解决方法:

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

See the following tickets for workarounds:

雾里花 2024-10-10 11:38:51

我发现限制 XYZ 图层的 maxZoom 级别的最简单方法是重写 getNumZoomLevels 方法:

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

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

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.

懒的傷心 2024-10-10 11:38:51

这里的其他答案参考版本 3 之前的 OpenLayers 版本。

使用 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
        })
    });

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

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

鸩远一方 2024-10-10 11:38:51

现在我在“视图”中使用它:

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

它工作正常

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

物价感观 2024-10-10 11:38:51

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

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.

我只土不豪 2024-10-10 11:38:51

我最终得到了这个解决方案,因为我无法弄清楚如何在我的设置中使用 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;
        }

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;
        }
芸娘子的小脾气 2024-10-10 11:38:51

我尝试过这样的事情,它对我有用:

map.newMoveTo = map.moveTo;
map.moveTo = function(lonlat,zoom,options){
    return(zoom>=maxZoom && zoom<=minZoom)?map.newMoveTo(lonlat,zoom,options):false;
};

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;
};
情场扛把子 2024-10-10 11:38:51

我找到的最佳答案(包括示例)位于 OpenLayers - 限制缩放级别的数量。我这里就不贴了,请看那边的说明。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文