openlayer 图层滑动

这个示例展示了如何在OpenLayers中创建图层滑动效果,通过一个滑块控制两个图层的显示,其中一个图层为OSM地图,另一个为Bing Maps的卫星图层。用户可以通过滑动条改变图层的覆盖度,实现地图的对比和查看。
摘要由CSDN通过智能技术生成

图层滑动

图层滑动贴图的示例。

 

<!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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值