OpenLayers TileLayer,如何在强制开启/关闭和基于缩放的可见性之间切换?

问题描述

我有一个按钮可以选择:

  • 强制可见
  • 强制隐形
  • 在预配置的缩放级别之间可见。

文档https://openlayers.org/en/latest/apidoc/module-ol_layer_Base-BaseLayer.html 不指定可见和最小/最大分辨率如何一起工作。 我需要 min/maxResolution 能够通过强制可见/不可见被否决。

我拥有的是这个。

  if (value == 0) { // by zoomlevel
    var mapLayer = wmsLayerDefs[index].mapLayerCfg;
    wmsLayer.setMinResolution(view.getResolutionForZoom(mapLayer.zoomLimits.max));
    wmsLayer.setMaxResolution(view.getResolutionForZoom(mapLayer.zoomLimits.min));
  } else if (value == 1) { //off
    wmsLayer.setMinResolution(view.getResolutionForZoom(0));
    wmsLayer.setMaxResolution(view.getResolutionForZoom(0));
  } else if (value == 2) { //on
    wmsLayer.setMinResolution(undefined);
    wmsLayer.setMaxResolution(undefined);
    wmsLayer.setVisible(true);
  }

选项 2 没有显示任何内容。我也试过如下,但它没有变得可见。我猜 50 的值不被接受。我不知道该夹什么。

  } else if (value == 2) { //on
    wmsLayer.setMinResolution(view.getResolutionForZoom(0));
    wmsLayer.setMaxResolution(view.getResolutionForZoom(50));
  }

选项 1 和 2 是我努力使其正确的地方

使用 ecmascript 5 和 ol-5.3.0.js (我不太精通 javascript)

解决方法

  } else if (value == 1) { //off
    wmsLayer.setMinResolution(Infinity);
    wmsLayer.setMaxResolution(0);
  } else if (value == 2) { //on
    wmsLayer.setMinResolution(0);
    wmsLayer.setMaxResolution(Infinity);
  }

应该足够了。对于“关闭”,两个设置都为零,或者两个都设置为 Infinity 也可以。