others_layerSwitch.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  8. <title>Roller Blinds</title>
  9. <style type="text/css">
  10. #handle {
  11. display: none;
  12. position: absolute;
  13. height: 100%;
  14. width: 6px;
  15. left: 50%;
  16. background: rgb(205, 205, 205);
  17. border: 1px solid rgb(112, 107, 107);
  18. z-index: 2000;
  19. cursor: col-resize;
  20. }
  21. #handleCenter {
  22. position: absolute;
  23. height: 32px;
  24. width: 24px;
  25. top: 50%;
  26. left: -10px;
  27. background: rgb(205, 205, 205);
  28. border: 1px solid rgb(112, 107, 107);
  29. z-index: 2000;
  30. background-image: url(images/drag.png);
  31. background-position: center;
  32. background-repeat: no-repeat;
  33. }
  34. .editPane {
  35. position: absolute;
  36. right: 50px;
  37. top: 50px;
  38. text-align: center;
  39. background: #FFF;
  40. z-index: 1000;
  41. }
  42. </style>
  43. </head>
  44. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  45. <div class='panel panel-primary editPane' id='editPane' style="z-index: 99999">
  46. <div class='panel-heading'>
  47. <h5 class='panel-title text-center'>Roller Blinds</h5>
  48. </div>
  49. <div class='panel-body' id='params'>
  50. <p></p>
  51. <div align='right' class='button-group'>
  52. <input type='button' id='btn1' class='btn btn-primary' data-i18n="[value]resources.btn_openSwitch" onclick="openLayerSwitch()"/>
  53. <input type='button' id='btn2' class='btn btn-primary' data-i18n="[value]resources.btn_closeSwitch" onclick="closeLayerSwitch()"/>
  54. </div>
  55. </div>
  56. </div>
  57. <div id="handle" data-i18n="[title]resources.text_lookUpLayer" onmousedown="drag(event)">
  58. <div id="handleCenter"></div>
  59. </div>
  60. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  61. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  62. <script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
  63. <script type="text/javascript">
  64. var map, layer,baseLayer, c, b,
  65. leftStart = 0, topStart = 0, leftEnd = 0, topEnd = 0,
  66. mapTop, mapRight, mapButtom, mapLeft, handleLeft,
  67. useCanvas, mapRightBack, isFirstDrag = false, isZoom = false, isMove = false,
  68. host = window.isLocal ? window.server : "https://iserver.supermap.io";
  69. url1 = host + "/iserver/services/map-world/rest/maps/World";
  70. url2 = host + "/iserver/services/map-world/rest/maps/世界地图_Night";
  71. /*
  72. * 不支持canvas的浏览器不能运行该范例
  73. * android 设备也不能运行该范例*/
  74. var broz = SuperMap.Util.getBrowser();
  75. if (!document.createElement('canvas').getContext) {
  76. widgets.alert.showAlert(resources.msg_supportCanvas, false);
  77. } else if (broz.device === 'android') {
  78. widgets.alert.showAlert(resources.msg_supportEquipment, false);
  79. }
  80. map = new SuperMap.Map("map", {
  81. controls: [
  82. new SuperMap.Control.ScaleLine(),
  83. new SuperMap.Control.Zoom(),
  84. new SuperMap.Control.Navigation({
  85. dragPanOptions: {
  86. enableKinetic: true
  87. }
  88. })]
  89. });
  90. //图层相互叠加
  91. map.allOverlays = true;
  92. //初始化底图
  93. baseLayer = new SuperMap.Layer.TiledDynamicRESTLayer("世界地图", url1, {
  94. transparent: false,
  95. cacheEnabled: true,
  96. redirect: true
  97. }, {maxResolution: "auto"});
  98. baseLayer.isBaseLayer = true;
  99. baseLayer.events.on({
  100. "layerInitialized": function () {
  101. var center = new SuperMap.LonLat(11733502.481499, 4614406.969325);
  102. map.addLayer(baseLayer);
  103. map.setCenter(center, 1);
  104. }
  105. });
  106. //初始化卷帘图层显示范围
  107. handleLeft = map.size.w * 0.5;
  108. mapTop = "0px";
  109. mapButtom = map.size.h + "px";
  110. mapLeft = "0px";
  111. mapRightBack = handleLeft;
  112. //初始化图层,此图层在最上层,做卷帘图层
  113. layer = new SuperMap.Layer.TiledDynamicRESTLayer("世界地图_Night", url2, {
  114. transparent: false,
  115. cacheEnabled: true,
  116. redirect: true
  117. }, {maxResolution: "auto", useCanvas: false});
  118. //关闭卷帘
  119. function closeLayerSwitch() {
  120. document.getElementById('handle').style.display = "none";
  121. if (map.getLayerIndex(layer) != -1) {
  122. map.removeLayer(layer);
  123. }
  124. }
  125. //打开卷帘
  126. function openLayerSwitch() {
  127. if (map.getLayerIndex(layer) == -1) {
  128. document.getElementById('handle').style.display = "block";
  129. addLayer();
  130. }
  131. }
  132. function addLayer() {
  133. map.addLayer(layer);
  134. useCanvas = layer.useCanvas;
  135. handle = document.getElementById("handle");
  136. //初始化卷帘的位置
  137. handle.style.left = mapRightBack + "px";
  138. //判断上次关闭前是否拖动过地图
  139. if (isMove) {
  140. mapMoveClip(0, 0);
  141. isMove = false;
  142. }
  143. else {
  144. clip(handleLeft);
  145. }
  146. //卷帘不支持非Canvas图层,非Canvas图层需要手动实现卷帘
  147. //监听地图拖动事件
  148. if (!useCanvas) {
  149. map.events.on({
  150. "movestart": movestart,
  151. "move": move
  152. });
  153. }
  154. }
  155. //使图层显示范围随着卷帘的拖动而变化
  156. function clip(left) {
  157. mapRight = left + "px";
  158. //如果在缩放后直接拖动卷帘div,卷帘图层显示范围右侧数值需要进行修正
  159. if (isZoom && isFirstDrag) {
  160. mapRight = (left + leftEnd) + "px";
  161. }
  162. var rect = "rect(" + mapTop + "," + mapRight + "," + mapButtom + "," + mapLeft + ")";
  163. layer.div.style.clip = rect;
  164. }
  165. //拖动卷帘
  166. function drag(e) {
  167. var objectDiv = document.getElementById("handle");
  168. //取消事件的默认行为
  169. e.preventDefault();
  170. if (document.all) { //鼠标捕获
  171. objectDiv.setCapture();
  172. }
  173. b = e.clientX - parseInt(objectDiv.style.left);
  174. console.log(b);
  175. //鼠标拖动
  176. document.onmousemove = function (d) {
  177. if (!d) d = event;
  178. c = (d.clientX - b);
  179. //使卷帘在地图内
  180. if (c > map.size.w - 7) {
  181. c = map.size.w - 7;
  182. }
  183. if (c < 0) {
  184. c = 0;
  185. }
  186. if (objectDiv) {
  187. objectDiv.style.left = c + "px";
  188. //如果使用非Canvas图层,在拖动地图后,再次拖动卷帘div,图层显示范围出错,这里修正图层显示范围右侧数值
  189. if (!useCanvas) {
  190. mapRightBack = c;
  191. c = c - leftEnd;
  192. isFirstDrag = true;
  193. }
  194. handleLeft = c; //记录滚动条的位置,以便再次打开时为拖动后的位置
  195. console.log(handleLeft);
  196. clip(c);
  197. }
  198. };
  199. //拖动完成后
  200. document.onmouseup = function () {
  201. if (document.all) {
  202. //释放鼠标捕获
  203. objectDiv.releaseCapture();
  204. }
  205. objectDiv = null;
  206. };
  207. }
  208. //地图drag,pan或者zoom操作开始时执行该函数,此时记录卷帘图层显示范围偏移量
  209. function movestart(evt) {
  210. var me = this;
  211. var parsePos = parse(evt.element.childNodes[0].style.left, evt.element.childNodes[0].style.top);
  212. leftStart = parsePos.left;
  213. topStart = parsePos.top;
  214. }
  215. //地图drag,pan或者zoom操作开始后执行该函数,此时计算卷帘图层显示范围偏移量,如果没有进行缩放,计算偏移量后,设置卷帘图层left,top,right,bottom显示数值
  216. //如果进行缩放,则left,top,bottom重置为默认值,right设置为当前卷帘div所在位置数值
  217. function move(evt) {
  218. if (!evt.zoomChanged) {
  219. isZoom = false;
  220. isMove = true;
  221. isFirstDrag = false;
  222. var parsePos = parse(evt.element.childNodes[0].style.left, evt.element.childNodes[0].style.top);
  223. leftEnd = parsePos.left;
  224. topEnd = parsePos.top;
  225. var dx = -(leftEnd - leftStart);
  226. var dy = -(topEnd - topStart);
  227. mapMoveClip(dx, dy);
  228. } else {
  229. isZoom = true;
  230. mapTop = "0px";
  231. mapRight = mapRightBack + "px";
  232. mapButtom = map.size.h + "px";
  233. mapLeft = "0px";
  234. var rect = "rect(" + mapTop + "," + mapRight + "," + mapButtom + "," + mapLeft + ")";
  235. layer.div.style.clip = rect;
  236. }
  237. }
  238. //设置非Canvas卷帘图层可见范围
  239. function mapMoveClip(dx, dy) {
  240. var posString = layer.div.style.clip.replace(/rect\(|\)|px/g, "");
  241. var pos = posString.split(" ");
  242. mapTop = (parseInt(pos[0]) + dy) + "px";
  243. mapRight = (parseInt(pos[1]) + dx) + "px";
  244. mapButtom = (parseInt(pos[2]) + dy) + "px";
  245. mapLeft = (parseInt(pos[3]) + dx) + "px";
  246. var rect = "rect(" + mapTop + "," + mapRight + "," + mapButtom + "," + mapLeft + ")";
  247. layer.div.style.clip = rect;
  248. leftStart = leftEnd;
  249. topStart = topEnd;
  250. }
  251. //将传入的数值取整
  252. function parse(left, top) {
  253. var left = left,
  254. top = top;
  255. if (left != undefined && top != undefined) {
  256. left = parseInt(left.replace(/px/g, ""));
  257. top = parseInt(top.replace(/px/g, ""));
  258. } else {
  259. left = 0;
  260. top = 0;
  261. }
  262. return {
  263. left: left,
  264. top: top
  265. };
  266. }
  267. </script>
  268. </body>
  269. </html>