Search code examples
javascripttimeopenlayerstile

OpenLayers time to layer (time control)


I am new to OpenLayers and I would like to have something takes (date+time) and to get (layer) on the map

E.g I have layers for the Weather in (15.02.2010), (16.02.2010) and so on.

for example.. user enter (date+time) and the result (layer of the same date on the map)

Are there any ideas? Example? API!


Solution

  • Adapted from the OpenLayers example:

    <input type="text" name="customParam" id="customParam">
    <button onclick="showIt()">show it</button>
    
    <script>
      let source = null;
    
      const map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        view: new ol.View({
            center: [0, 0],
            zoom: 2
        })
      });
    
      function showIt() {
        const customParam = document.getElementById("customParam").value;
        const isNew = (!source);
    
        if(isNew) {
          source = new ol.source.XYZ();
        }
    
        source.setUrl(`https://{a-c}.MY-TILES.com/{z}/{x}/{y}.png?myTimeParam=${customParam}`);
        source.refresh();
    
        if(isNew) {
          map.addLayer(new ol.layer.Tile({ source: source }));
        }
      }
    </script>