5

我整天都在做这个,老实说,我没有想法。我有一些 WMS 图层,我想根据当前的缩放级别显示/不显示。是的,我已经浏览了 API 文档,它们似乎很清楚,但是我遵循了建议的所有内容,但没有得到想要的结果:(

这是我查看的来源之一:http://trac.osgeo.org/openlayers/wiki/SettingZoomLevels

然后更糟糕的是,我发现如果您在加载时显示 Open Street Map 基础层,它似乎会限制您对地图的 numZoomLevels 的控制,这正是我所需要的,因为我确实想将其用作我的加载基础层...

所以我的问题是:

我究竟做错了什么?在加载时使用 Open Street Map 基础图层时,是否真的没有控制缩放级别的解决方法?还是有什么我不知道的?

以下是我的一些代码尝试: 采取 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}
                );

采取 2: tib_villages 图层应该只在缩放级别为 8-10 时显示,地图应该只有 10 个缩放级别,而是有 19 个,因为 Open Street Map Layer 强制执行它,不起作用!

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

;

采取 3:在加载时去掉 Open Street Map 基础图层后,地图只有指定的 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}
                );

衷心感谢您的所有建议!

艾尔谢

4

2 回答 2

9

尝试使用minResolutionandmaxResolution而不是minZoomLevel. 它通常工作正常。map.getResolution()如果您调用方法,您可以获得任何变焦杆的分辨率。

另一种选择是监听OpenLayers.Map'szoomend事件并相应地切换图层可见性。像这样的东西:

map.events.on({ "zoomend": function (e) {
    if (this.getZoom() > 2) {
      layer1.setVisibility(false);
      layer2.setVisibility(true);
    }
    else {
      layer2.setVisibility(false);
      layer1.setVisibility(true);
    }
  }
});
于 2010-10-25T09:49:21.903 回答
5

我建议在图层上使用 maxScale 和 minScale 而不是分辨率,但这可能是一个品味问题。:-) 我与分辨率值无关,但是当其他人查看您的代码时,从长远来看,比例很容易理解和维护。

于 2010-12-20T18:28:31.397 回答