【问题标题】:OpenLayers function to switch many layers on by a loopOpenLayers 函数通过循环打开多个层
【发布时间】:2017-12-06 14:06:27
【问题描述】:

我有一个图层切换控件,其中包含名为 (yyyy-mm-dd_hhmmss) 的(日期),当我单击它们时它们可以正常工作。

另外,我有输入(开始)和(结束)的标签和一个在这两个值之间循环层的按钮。

我需要一个function 来使此按钮激活图层(日期)或使它们按间隔循环打开

PS:这里我们可以使用title作为参考,displayInLayerSwitcher,truesetVisible(true)

我的代码如下

singleLayer = {}
singleLayer['1']	=	new ol.layer.Tile({
		title: "1",
			baseLayer: false,
			visible: false,
			source: new ol.source.XYZ({
			projection: 'EPSG:4326',
				wrapX: false,
				url: 'folder/yyyy-mm-dd_hhmmss/{z}/{x}/{-y}.png'
			})
	})
singleLayer['2'] = new ol.layer.Tile({
		title: "2",
			baseLayer: false,
			visible: false,
			source: new ol.source.XYZ({
			projection: 'EPSG:4326',
				wrapX: false,
				url: 'folder/yyyy-mm-dd_hhmmss/{z}/{x}/{-y}.png'
			})
		})

 singleLayer['3']	=	new ol.layer.Tile({
			title: "3",
				baseLayer: false,
				visible: false,
				source: new ol.source.XYZ({
				projection: 'EPSG:4326',
					wrapX: false,
					url: 'folder/yyyy-mm-dd_hhmmss/{z}/{x}/{-y}.png'
				})
		})
			 	 })


	var dates = new ol.layer.Group({
		openInLayerSwitcher: false,
		visible: false,
		layers:[
			singleLayer['1'],
			singleLayer['2'],
			singleLayer['3'],
		]
	});
<p>Loop Layers<p>
              <p>From date: <input type="text" name="fromdate" id="fromdate" value="yyyy-mm-dd_hhmmss"></p>
              <p>To date: <input type="text" name="todate" id="todate" value="yyyy-mm-dd_hhmmss"></p>
              <button onclick="loopLayer()">Loop layer by time interval</button>
              
function loopLayer() {
  .........
  }

【问题讨论】:

    标签: javascript switch-statement openlayers layer


    【解决方案1】:
    var time = new ol.layer.Group({ ....... });
    
    let intervalHandle = null;
    let activeLayerIndex = -1;
    
    function startAnimation() {
      // check if interval is already running
      if (!intervalHandle) {
        activeLayerIndex = 0;
        // make sure all layers of the group are hidden
        time.getLayers().forEach(layer => layer.setVisible(false));
    
        intervalHandle = setInterval(function() {
          const allLayers = time.getLayers();
          if (activeLayerIndex >= allLayers.getLength()) {
            // we reached the end, stop animating
            stopAnimation();
          }
          allLayers.item(activeLayerIndex).setVisible(true);
    
          if (activeLayerIndex > 0) {
            // hide the previous layer
            allLayers.item(activeLayerIndex -1).setVisible(false);
          }
          activeLayerIndex += 1;
        }, 500 /* milliseconds */);
      }
    }
    
    function stopAnimation() {
      if(intervalHandle) {
        clearInterval(intervalHandle);
        intervalHandle = null;
      }
    }
    

    【讨论】:

    • 我得到一个错误(未定义不是一个对象(评估'allLayers[activeLayerIndex].setVisible'))
    • 糟糕,我的错。 getLayers() 返回一个集合,而不是一个数组。固定
    • 好的,现在可以了,但是有两个问题。首先(我必须先手动打开layerswitchcontrol中的时间组,所以我们需要在循环工作之前添加切换它的功能。其次,只显示一个图层而不显示其他图层!
    • 默认显示组,删除visible: false,或在startAnimation中添加time.setVisible(true)。如果您想继续显示图层而不隐藏之前显示的图层,只需删除或使用注释 // hide the previous layer 注释该部分(第 23 到 26 行)
    • 我的意思是该功能只显示一层并停止,不显示其他层,所以我将这些行注释如下,它工作正常,当它到达末尾时它会自动停止
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    相关资源
    最近更新 更多