MouseTool.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <!-- Use correct character set. -->
  5. <meta charset="utf-8"/>
  6. <!-- Tell IE to use the latest, best version. -->
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
  8. <!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
  9. <meta
  10. name="viewport"
  11. content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
  12. />
  13. <title>Hello World!</title>
  14. <link rel="stylesheet" href="../../dist/index.css"/>
  15. <script src="../../dist/index.js"></script>
  16. <script src="../build/cesium/Cesium.js"></script>
  17. <link href="../build/cesium/Widgets/widgets.css" rel="stylesheet">
  18. <style>
  19. html,
  20. body,
  21. #cesiumContainer {
  22. width: 100%;
  23. height: 100%;
  24. margin: 0;
  25. padding: 0;
  26. overflow: hidden;
  27. position: relative;
  28. }
  29. .box {
  30. position: fixed;
  31. left: 0;
  32. top: 0;
  33. z-index: 1000;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="cesiumContainer"></div>
  39. <div class="box">
  40. <button class="btn" id="point">打点</button>
  41. <button class="btn" id="polyline">画线</button>
  42. <button class="btn" id="polygon">画多边形</button>
  43. <button class="btn" id="rule">测距</button>
  44. <button class="btn" id="area">测面</button>
  45. <button class="btn" id="rect">矩形</button>
  46. <button class="btn" id="clear">清除绘图</button>
  47. <button class="btn" id="clearBoth">关闭绘图</button>
  48. <button class="btn" id="plot">其他标绘</button>
  49. <button class="btn" id="pause">暂定绘图</button>
  50. <button class="btn" id="begin">开始绘图</button>
  51. <button class="btn" id="reset">重新创建</button>
  52. </div>
  53. <script>
  54. let map = new CTMapOl.cesiumComponent.ThrdimeInitMap({
  55. domId: 'cesiumContainer',
  56. center: [116,39],
  57. zoom: 8
  58. })
  59. let viewer = map._viewer;
  60. let mouseTool = new CTMapOl.cesiumComponent.MouseTool(viewer)
  61. document.getElementById("reset").onclick = function () {
  62. mouseTool.close(true);
  63. mouseTool = new CTMapOl.cesiumComponent.MouseTool(viewer)
  64. };
  65. // map.on('click', e => {
  66. // console.log('mapclick', e)
  67. // })
  68. document.getElementById("point").onclick = function () {
  69. mouseTool.marker({
  70. infinite: true,
  71. strokeColor: 'pink',
  72. strokeOpacity: 1,
  73. outlineColor: 'pink',
  74. outlineColorOpacity: 1,
  75. borderWeight: 1,
  76. strokeWeight: 10,
  77. }, e => {
  78. console.log(e)
  79. })
  80. };
  81. document.getElementById("polyline").onclick = function () {
  82. mouseTool.polyline({
  83. infinite: true,
  84. strokeColor: '#00D3FC',
  85. strokeOpacity: 0.5,
  86. outlineColor: '#00B2D5',
  87. outlineColorOpacity: 0.5,
  88. borderWeight: 1,
  89. strokeWeight: 2,
  90. draw: (e) => {
  91. console.log('polyline', e)
  92. if (e?.distance > 100) {
  93. console.log('超过了限制')
  94. e.cancel();
  95. }
  96. }
  97. }, e => {
  98. console.log(e)
  99. })
  100. };
  101. document.getElementById("polygon").onclick = function () {
  102. mouseTool.polygon({
  103. infinite: true,
  104. strokeColor: '#00D3FC',
  105. strokeOpacity: 0.5,
  106. strokeWeight: 2,
  107. fillColor: '#00B2D5',
  108. fillOpacity: 0.5,
  109. heightReference: 1,
  110. draw: (e) => {
  111. console.log('polygon', e)
  112. if (e?.area > 5000) {
  113. console.log('超过了限制')
  114. e.cancel();
  115. }
  116. console.log(1)
  117. }
  118. }, e => {
  119. console.log(e)
  120. })
  121. };
  122. document.getElementById("clear").onclick = function () {
  123. mouseTool.close();
  124. };
  125. document.getElementById("clearBoth").onclick = function () {
  126. mouseTool.close(true);
  127. };
  128. document.getElementById("rule").onclick = function () {
  129. // startMarkerOptions: {//可缺省
  130. // icon: "//webapi.amap.com/theme/v1.3/markers/b/start.png",
  131. // iconSize: [19, 31],
  132. // offset: [9, 31]
  133. // },
  134. // endMarkerOptions: {//可缺省
  135. // icon: "//webapi.amap.com/theme/v1.3/markers/b/end.png",
  136. // iconSize: [19, 31],
  137. // offset: [9, 31]
  138. // },
  139. // midMarkerOptions: {//可缺省
  140. // icon: "//webapi.amap.com/theme/v1.3/markers/b/mid.png",
  141. // iconSize: [19, 31],
  142. // offset: [9, 31]
  143. // },
  144. mouseTool.rule({
  145. lineOptions: {//可缺省
  146. strokeStyle: "solid",
  147. strokeColor: "orange",
  148. strokeOpacity: 1,
  149. strokeWeight: 5
  150. },
  151. pointOption: {
  152. strokeColor: 'red',
  153. strokeOpacity: 1,
  154. outlineColor: 'yellow',
  155. outlineColorOpacity: 1,
  156. borderWeight: 1,
  157. strokeWeight: 3,
  158. },
  159. inforWindow: {
  160. anchor: ['50%', 40],
  161. content: '<div style="padding: 3px 10px;background: white;"><span class="distance">距离</span><span class="close">x</span><span class="trash">x</span></div>',
  162. className: 'abc100'
  163. },
  164. }, e => {
  165. console.log(e)
  166. });
  167. };
  168. document.getElementById("area").onclick = function () {
  169. mouseTool.measureArea({
  170. strokeColor: 'red',
  171. strokeOpacity: 0.5,
  172. strokeWeight: 5,
  173. fillColor: 'pink',
  174. fillOpacity: 0.5,
  175. inforWindow: {
  176. anchor: ['50%', 40],
  177. content: '<div style="padding: 3px 10px;background: white;"><span class="area">面积</span><span class="trash">x</span></div>',
  178. className: 'abc100'
  179. },
  180. }, e => {
  181. console.log('测面', e)
  182. });
  183. };
  184. document.getElementById("rect").onclick = function () {
  185. mouseTool.rectangle({
  186. strokeColor: 'red',
  187. strokeOpacity: 0.5,
  188. strokeWeight: 5,
  189. fillColor: 'pink',
  190. fillOpacity: 0.5,
  191. draw: (e) => {
  192. if (e?.area > 10000) {
  193. console.log('超过了限制')
  194. e.cancel();
  195. }
  196. }
  197. }, e => {
  198. console.log('矩形', e)
  199. });
  200. };
  201. document.getElementById("pause").onclick = function () {
  202. mouseTool.pauseDraw();
  203. };
  204. document.getElementById("begin").onclick = function () {
  205. mouseTool.beginDraw();
  206. };
  207. document.getElementById("plot").onclick = function () {
  208. // 用户获取的树形数据
  209. // let symbolList = mouseTool.getSymbolData({path: '../assets/SymbolIcon'}, true);
  210. let symbolList = mouseTool.getSymbolData({path: '../assets/SymbolIcon'}, true);
  211. let target = document.getElementById('symbolBox').getElementsByTagName('img');
  212. for (let i=0;i<target.length;i++){
  213. target[i].addEventListener('click', e => {
  214. let par = e.target.parentNode;
  215. let type = par.getAttribute('data-type');
  216. console.log('当前点击的类型', type)
  217. console.log('当前点击的id', par.getAttribute('id'))
  218. mouseTool[type]({
  219. id: par.getAttribute('id'),
  220. strokeColor: 'red',
  221. strokeOpacity:1,
  222. strokeWeight: 4,
  223. fillColor: 'red',
  224. fillOpacity: 0,
  225. pointColor:'red',
  226. pointRadius: 4,
  227. pointOpacity: 1,
  228. infinite: true,
  229. });
  230. })
  231. }
  232. };
  233. </script>
  234. </body>
  235. </html>