controler_layerswitch.html 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta charset="UTF-8">
  8. <title data-i18n="resources.title_layerSwitch"></title>
  9. <script type="text/javascript" src="../js/include-web.js"></script>
  10. <script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
  11. </head>
  12. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:95%; position: absolute;top: 0;">
  13. <div id="map" style="width: 100%;height:95%"></div>
  14. <input id="swipe" type="range" style="width: 100%;">
  15. <script type="text/javascript">
  16. var host = window.isLocal ? window.server : "https://iserver.supermap.io";
  17. var worldurl = host + '/iserver/services/map-world/rest/maps/World',
  18. worldNighturl = host + '/iserver/services/map-world/rest/maps/世界地图_Night';
  19. var world = new ol.layer.Tile({
  20. source: new ol.source.TileSuperMapRest({
  21. url: worldurl
  22. }),
  23. projection: 'EPSG:4326'
  24. });
  25. var worldNight = new ol.layer.Tile({
  26. source: new ol.source.TileSuperMapRest({
  27. url: worldNighturl
  28. }),
  29. projection: 'EPSG:4326'
  30. });
  31. var map = new ol.Map({
  32. layers: [world, worldNight],
  33. target: 'map',
  34. controls: ol.control.defaults({attributionOptions: {collapsed: false}}),
  35. view: new ol.View({
  36. center: [0, 0],
  37. zoom: 3,
  38. projection: 'EPSG:4326',
  39. multiWorld: true
  40. })
  41. });
  42. var swipe = document.getElementById('swipe');
  43. // ol6 废除了 precompose,由 prerender 事件替换
  44. worldNight.on('prerender', function (event) {
  45. var ctx = event.context;
  46. var width = ctx.canvas.width * (swipe.value / 100);
  47. ctx.save();
  48. ctx.beginPath();
  49. ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height);
  50. ctx.clip();
  51. });
  52. // ol6 废除了 postcompose,由 postrender 事件替换
  53. worldNight.on('postrender', function (event) {
  54. var ctx = event.context;
  55. ctx.restore();
  56. });
  57. swipe.addEventListener('input', function () {
  58. map.render();
  59. }, false);
  60. </script>
  61. </body>
  62. </html>