图层滑动

openlayer 图层滑动

图层滑动贴图的示例。

 

<!DOCTYPE html><html>

  <head>

    <title>Layer Swipe</title>

    <link rel="stylesheet" href="https://openlayers.org/en/v5.1.3/css/ol.css" type="text/css">

    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->

    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>

  </head>

  <body>

    <div id="map" class="map"></div>

    <input id="swipe" type="range" style="width: 100%">

    <script>

      import Map from 'ol/Map.js';

      import View from 'ol/View.js';

      import {defaults as defaultControls} from 'ol/control.js';

      import TileLayer from 'ol/layer/Tile.js';

      import BingMaps from 'ol/source/BingMaps.js';

      import OSM from 'ol/source/OSM.js';

 

      var osm = new TileLayer({

        source: new OSM()

      });

      var bing = new TileLayer({

        source: new BingMaps({

          key: 'Your Bing Maps Key from http://www.bingmapsportal.com/ here',

          imagerySet: 'Aerial'

        })

      });

 

      var map = new Map({

        layers: [osm, bing],

        target: 'map',

        controls: defaultControls({

          attributionOptions: {

            collapsible: false

          }

        }),

        view: new View({

          center: [0, 0],

          zoom: 2

        })

      });

 

      var swipe = document.getElementById('swipe');

 

      bing.on('precompose', function(event) {

        var ctx = event.context;

        var width = ctx.canvas.width * (swipe.value / 100);

 

        ctx.save();

        ctx.beginPath();

        ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height);

        ctx.clip();

      });

 

      bing.on('postcompose', function(event) {

        var ctx = event.context;

        ctx.restore();

      });

 

      swipe.addEventListener('input', function() {

        map.render();

      }, false);

    </script>

  </body></html>

 

相关文章: