javascript 根据缩放级别显示 WMS 图层
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4000734/
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
Display WMS Layer Based on Zoom Level
提问by elshae
I've been at this all day long and honestly I'm out of ideas. I have some WMS layers that I would like to display/not display depending on the current zoom level. Yes, I have went through the API docs and they seem to be clear as day, but I follow everything that is suggested and I'm not getting the results desired :(
我整天都在做这件事,老实说,我已经没有想法了。我有一些 WMS 图层,我想根据当前的缩放级别显示/不显示。是的,我已经浏览了 API 文档,它们似乎很清楚,但我遵循了建议的所有内容,但没有得到所需的结果:(
This was one of the sources that I looked at: http://trac.osgeo.org/openlayers/wiki/SettingZoomLevels
这是我查看的来源之一:http://trac.osgeo.org/openlayers/wiki/SettingZoomLevels
Then to make matters even worse I found out that if you have an Open Street Map base layer displaying on load it seems to limit your control over the map's numZoomLevels, just what I needed, since I DO want to use this as my loading base layer...
然后更糟糕的是,我发现如果你有一个在加载时显示的开放街道地图基础层,它似乎限制了你对地图的 numZoomLevels 的控制,这正是我需要的,因为我想用它作为我的加载基础层...
So my questions are:
所以我的问题是:
What am I doing wrong? Is it true that there really is no workaround on the control of zoom levels when using an Open Street Map base layer on load? Or is there something I just don't know?
我究竟做错了什么?在加载时使用开放街道地图基础图层时,真的没有控制缩放级别的解决方法吗?或者有什么我不知道的?
Here are some of my code attempts: Take 1: tib_villages layer should only show when the zoom level is 8-10, doesn't work!
下面是我的一些代码尝试: Take 1: tib_villages 层应该只在缩放级别为 8-10 时显示,不起作用!
var options = {
controls: [new OpenLayers.Control.Navigation()], //Needed to use GeoExt controls such as the zoomslider
maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34),
units: 'm',
numZoomLevels: null, //setting the map's zoom levels to null
allOverlays: false
}
var osm = new OpenLayers.Layer.OSM(); //MY base layer
//MY overlay layer
var tib_villages = new OpenLayers.Layer.WMS(
"Villages", "http://localhost:8080/geoserver/wms", {layers: 'cite:tib_villages', transparent: true, numZoomLevels: 10, minZoomLevel: 8}, {isBaseLayer: false, displayInLayerSwitcher: true, visibility: true}
);
Take 2: tib_villages layer should only show when the zoom level is 8-10, map should only have 10 zoom levels, but instead has 19 as the Open Street Map Layer enforces it to, doesn't work!
取 2:tib_villages 图层应该只在缩放级别为 8-10 时显示,地图应该只有 10 个缩放级别,但是因为 Open Street Map Layer 强制执行它而具有 19,不起作用!
var options = {
controls: [new OpenLayers.Control.Navigation()], //Needed to use GeoExt controls such as the zoomslider
maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34),
units: 'm',
numZoomLevels: 10, //setting the map's zoom levels to 10 only
allOverlays: false
}
var osm = new OpenLayers.Layer.OSM(); //MY base layer
//MY overlay layer
var tib_villages = new OpenLayers.Layer.WMS(
"Villages", "http://localhost:8080/geoserver/wms", {layers: 'cite:tib_villages', transparent: true, numZoomLevels: null, minZoomLevel: 8}, {isBaseLayer: false, displayInLayerSwitcher: true, visibility: true}
)
;
;
Take 3: After getting rid of the Open Street Map base layer on load, the map only has 10 zoom levels as specified, but tib_villages layer should only show when the zoom level is 8-10, doesn't work!
取3:在加载时去掉Open Street Map base layer后,地图只有指定的10个缩放级别,但是tib_villages图层应该只在缩放级别为8-10时显示,不起作用!
var options = {
controls: [new OpenLayers.Control.Navigation()], //Needed to use GeoExt controls such as the zoomslider
maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34),
units: 'm',
numZoomLevels: 10, //setting the map's zoom levels to 10
allOverlays: false
}
//MY overlay layer
var tib_villages = new OpenLayers.Layer.WMS(
"Villages", "http://localhost:8080/geoserver/wms", {layers: 'cite:tib_villages', transparent: true, numZoomLevels: 10, minZoomLevel: 8}, {isBaseLayer: false, displayInLayerSwitcher: true, visibility: true}
);
All of your suggestions are sincerely appreciated!
真诚地感谢您的所有建议!
elshae
艾尔莎
回答by igorti
Try to use minResolutionand maxResolutioninstead of minZoomLevel. It usually works fine. You can get resolution for any zoom lever if you call map.getResolution()method.
尝试使用minResolution和maxResolution而不是minZoomLevel。它通常工作正常。如果您调用map.getResolution()方法,您可以获得任何变焦杆的分辨率。
Another option is to listen to OpenLayers.Map's zoomendevent and toggle layer visibility accordingly. Something like this:
另一种选择是侦听OpenLayers.Map的zoomend事件并相应地切换图层可见性。像这样的东西:
map.events.on({ "zoomend": function (e) {
if (this.getZoom() > 2) {
layer1.setVisibility(false);
layer2.setVisibility(true);
}
else {
layer2.setVisibility(false);
layer1.setVisibility(true);
}
}
});
回答by Niklas Wulff
I would recommend using the maxScale and minScale on the layers instead of resolution, but that maybe is a question of taste. :-) I can't relate to a resolution value, but a scale is easy to understand and to maintain in the longer run, when others look at your code.
我建议在图层上使用 maxScale 和 minScale 而不是分辨率,但这可能是一个品味问题。:-) 我无法与分辨率值相关联,但是当其他人查看您的代码时,从长远来看,比例很容易理解和维护。

