graphicManager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //entity示例中的图层通用js
  2. //在图层级处理一些事物
  3. function initLayerManager(graphicLayer) {
  4. //在layer上绑定监听事件
  5. graphicLayer.on(mars3d.EventType.click, function (event) {
  6. console.log("监听layer,单击了矢量对象", event);
  7. });
  8. // graphicLayer.on(mars3d.EventType.mouseOver, function(event) {
  9. // console.log('监听layer,鼠标移入了矢量对象', event)
  10. // })
  11. // graphicLayer.on(mars3d.EventType.mouseOut, function(event) {
  12. // console.log('监听layer,鼠标移出了矢量对象', event)
  13. // })
  14. //可在图层上绑定popup,对所有加到这个图层的矢量数据都生效
  15. bindLayerPopup(graphicLayer);
  16. $("#chkPopup").change(function () {
  17. let val = $(this).is(":checked");
  18. if (val) {
  19. bindLayerPopup(graphicLayer);
  20. } else {
  21. graphicLayer.unbindPopup();
  22. }
  23. });
  24. //可在图层上绑定tooltip,对所有加到这个图层的矢量数据都生效
  25. $("#chkTooltip").change(function () {
  26. let val = $(this).is(":checked");
  27. if (val) {
  28. graphicLayer.bindTooltip("我是layer上绑定的Tooltip");
  29. } else {
  30. graphicLayer.unbindTooltip();
  31. }
  32. });
  33. //可在图层绑定右键菜单,对所有加到这个图层的矢量数据都生效
  34. bindLayerContextMenu(graphicLayer);
  35. $("#chkContextMenu").change(function () {
  36. let val = $(this).is(":checked");
  37. if (val) {
  38. bindLayerContextMenu(graphicLayer);
  39. } else {
  40. graphicLayer.unbindContextMenu(true);
  41. }
  42. });
  43. $("#chkHasEdit").change(function () {
  44. let val = $(this).is(":checked");
  45. graphicLayer.hasEdit = val; //启用编辑
  46. });
  47. $("#chkShow").change(function () {
  48. let val = $(this).is(":checked");
  49. graphicLayer.show = val; //显示隐藏
  50. });
  51. $("#btnClear").click(function () {
  52. graphicLayer.clear();
  53. });
  54. $("#btnExpFile").click(function () {
  55. // window.layer.msg('该功能在开发完善中……')
  56. // return
  57. if (graphicLayer.length === 0) {
  58. window.layer.msg("当前没有标注任何数据,无需保存!");
  59. return;
  60. }
  61. let geojson = graphicLayer.toGeoJSON();
  62. haoutil.file.downloadFile("我的标注.json", JSON.stringify(geojson));
  63. });
  64. $("#btnImpFile").click(function () {
  65. $("#input_draw_file").click();
  66. });
  67. function clearSelectFile() {
  68. if (!window.addEventListener) {
  69. document.getElementById("input_draw_file").outerHTML += ""; //IE
  70. } else {
  71. document.getElementById("input_draw_file").value = ""; //FF
  72. }
  73. }
  74. $("#input_draw_file").change(function (e) {
  75. let file = this.files[0];
  76. let fileName = file.name;
  77. let fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase();
  78. if (fileType == "json" || fileType == "geojson") {
  79. let reader = new FileReader();
  80. reader.readAsText(file, "UTF-8");
  81. reader.onloadend = function (e) {
  82. let geojson = this.result;
  83. geojson = simplifyGeoJSON(geojson); //简化geojson的点
  84. graphicLayer.loadGeoJSON(geojson, {
  85. flyTo: true,
  86. });
  87. clearSelectFile();
  88. };
  89. } else if (fileType == "kml") {
  90. let reader = new FileReader();
  91. reader.readAsText(file, "UTF-8");
  92. reader.onloadend = function (e) {
  93. let strkml = this.result;
  94. kgUtil.toGeoJSON(strkml).then((geojson) => {
  95. geojson = simplifyGeoJSON(geojson); //简化geojson的点
  96. console.log("kml2geojson", geojson);
  97. graphicLayer.loadGeoJSON(geojson, {
  98. flyTo: true,
  99. // symbol: function (attr, style, featue) {
  100. // let geoType = featue.geometry?.type
  101. // if (geoType == 'Point') {
  102. // return {
  103. // image: 'img/marker/di3.png',
  104. // verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  105. // scale: 0.4,
  106. // label: {
  107. // text: attr.name,
  108. // font_size: 18,
  109. // color: '#ffffff',
  110. // outline: true,
  111. // outlineColor: '#000000',
  112. // pixelOffsetY: -50,
  113. // scaleByDistance: true,
  114. // scaleByDistance_far: 990000,
  115. // scaleByDistance_farValue: 0.3,
  116. // scaleByDistance_near: 10000,
  117. // scaleByDistance_nearValue: 1,
  118. // },
  119. // }
  120. // }
  121. // return style
  122. // },
  123. });
  124. clearSelectFile();
  125. });
  126. clearSelectFile();
  127. };
  128. } else if (fileType == "kmz") {
  129. //加载input文件控件的二进制流
  130. kgUtil.toGeoJSON(file).then((geojson) => {
  131. geojson = simplifyGeoJSON(geojson); //简化geojson的点
  132. console.log("kmz2geojson", geojson);
  133. graphicLayer.loadGeoJSON(geojson, {
  134. flyTo: true,
  135. });
  136. clearSelectFile();
  137. });
  138. } else {
  139. window.layer.msg("暂不支持 " + fileType + " 文件类型的数据!");
  140. clearSelectFile();
  141. }
  142. });
  143. }
  144. //简化geojson的坐标
  145. function simplifyGeoJSON(geojson) {
  146. try {
  147. geojson = turf.simplify(geojson, { tolerance: 0.000001, highQuality: true, mutate: true });
  148. } catch (e) {
  149. //
  150. }
  151. return geojson;
  152. }
  153. function bindLayerPopup(graphicLayer) {
  154. graphicLayer.bindPopup(function (event) {
  155. let attr = event.graphic?.attr || {};
  156. attr.test1 = "测试属性";
  157. // attr["视频"] = `<video src='//data.mars3d.cn/file/video/lukou.mp4' controls autoplay style="width: 300px;" ></video>`;
  158. return mars3d.Util.getTemplateHtml({ title: "layer上绑定的Popup", template: "all", attr: attr });
  159. });
  160. }
  161. function bindLayerContextMenu(graphicLayer) {
  162. graphicLayer.bindContextMenu([
  163. {
  164. text: "开始编辑对象",
  165. iconCls: "fa fa-edit",
  166. show: function (e) {
  167. let graphic = e.graphic;
  168. if (!graphic || !graphic.startEditing) {
  169. return false;
  170. }
  171. return !graphic.isEditing;
  172. },
  173. callback: function (e) {
  174. let graphic = e.graphic;
  175. if (!graphic) {
  176. return false;
  177. }
  178. if (graphic) {
  179. graphicLayer.startEditing(graphic);
  180. }
  181. },
  182. },
  183. {
  184. text: "停止编辑对象",
  185. iconCls: "fa fa-edit",
  186. show: function (e) {
  187. let graphic = e.graphic;
  188. if (!graphic) {
  189. return false;
  190. }
  191. return graphic.isEditing;
  192. },
  193. callback: function (e) {
  194. let graphic = e.graphic;
  195. if (!graphic) {
  196. return false;
  197. }
  198. if (graphic) {
  199. graphicLayer.stopEditing(graphic);
  200. }
  201. },
  202. },
  203. {
  204. text: "删除对象",
  205. iconCls: "fa fa-trash-o",
  206. show: (event) => {
  207. let graphic = event.graphic;
  208. if (!graphic || graphic.isDestroy) {
  209. return false;
  210. } else {
  211. return true;
  212. }
  213. },
  214. callback: function (e) {
  215. let graphic = e.graphic;
  216. if (!graphic) {
  217. return;
  218. }
  219. graphicLayer.removeGraphic(graphic);
  220. },
  221. },
  222. {
  223. text: "计算长度",
  224. iconCls: "fa fa-medium",
  225. show: function (e) {
  226. let graphic = e.graphic;
  227. if (!graphic) {
  228. return false;
  229. }
  230. return (
  231. graphic.type === "polyline" ||
  232. graphic.type === "polylineP" ||
  233. graphic.type === "curve" ||
  234. graphic.type === "curveP" ||
  235. graphic.type === "polylineVolume" ||
  236. graphic.type === "polylineVolumeP" ||
  237. graphic.type === "corridor" ||
  238. graphic.type === "corridorP" ||
  239. graphic.type === "wall" ||
  240. graphic.type === "wallP"
  241. );
  242. },
  243. callback: function (e) {
  244. let graphic = e.graphic;
  245. let strDis = mars3d.MeasureUtil.formatDistance(graphic.distance);
  246. haoutil.alert("该对象的长度为:" + strDis);
  247. },
  248. },
  249. {
  250. text: "计算周长",
  251. iconCls: "fa fa-medium",
  252. show: function (e) {
  253. let graphic = e.graphic;
  254. if (!graphic) {
  255. return false;
  256. }
  257. return (
  258. graphic.type === "circle" ||
  259. graphic.type === "circleP" ||
  260. graphic.type === "rectangle" ||
  261. graphic.type === "rectangleP" ||
  262. graphic.type === "polygon" ||
  263. graphic.type === "polygonP"
  264. );
  265. },
  266. callback: function (e) {
  267. let graphic = e.graphic;
  268. let strDis = mars3d.MeasureUtil.formatDistance(graphic.distance);
  269. haoutil.alert("该对象的周长为:" + strDis);
  270. },
  271. },
  272. {
  273. text: "计算面积",
  274. iconCls: "fa fa-reorder",
  275. show: function (e) {
  276. let graphic = e.graphic;
  277. if (!graphic) {
  278. return false;
  279. }
  280. return (
  281. graphic.type === "circle" ||
  282. graphic.type === "circleP" ||
  283. graphic.type === "rectangle" ||
  284. graphic.type === "rectangleP" ||
  285. graphic.type === "polygon" ||
  286. graphic.type === "polygonP" ||
  287. graphic.type === "scrollWall" ||
  288. graphic.type === "water"
  289. );
  290. },
  291. callback: function (e) {
  292. let graphic = e.graphic;
  293. let strArea = mars3d.MeasureUtil.formatArea(graphic.area);
  294. haoutil.alert("该对象的面积为:" + strArea);
  295. },
  296. },
  297. ]);
  298. }
  299. //也可以在单个Graphic上做个性化管理及绑定操作
  300. function initGraphicManager(graphic) {
  301. //3.在graphic上绑定监听事件
  302. // graphic.on(mars3d.EventType.click, function(event) {
  303. // console.log('监听graphic,单击了矢量对象', event)
  304. // })
  305. // graphic.on(mars3d.EventType.mouseOver, function(event) {
  306. // console.log('监听graphic,鼠标移入了矢量对象', event)
  307. // })
  308. // graphic.on(mars3d.EventType.mouseOut, function(event) {
  309. // console.log('监听graphic,鼠标移出了矢量对象', event)
  310. // })
  311. //绑定Tooltip
  312. // graphic.bindTooltip('我是graphic上绑定的Tooltip') //.openTooltip()
  313. //绑定Popup
  314. var inthtml = `<table style="width: auto;">
  315. <tr>
  316. <th scope="col" colspan="2" style="text-align:center;font-size:15px;">我是graphic上绑定的Popup </th>
  317. </tr>
  318. <tr>
  319. <td>提示:</td>
  320. <td>这只是测试信息,可以任意html</td>
  321. </tr>
  322. </table>`;
  323. graphic.bindPopup(inthtml).openPopup();
  324. //绑定右键菜单
  325. graphic.bindContextMenu([
  326. {
  327. text: "删除对象[graphic绑定的]",
  328. iconCls: "fa fa-trash-o",
  329. callback: function (e) {
  330. let graphic = e.graphic;
  331. if (graphic) {
  332. graphic.remove();
  333. }
  334. },
  335. },
  336. ]);
  337. //测试 颜色闪烁
  338. if (graphic.startFlicker) {
  339. graphic.startFlicker({
  340. time: 20, //闪烁时长(秒)
  341. maxAlpha: 0.5,
  342. color: Cesium.Color.YELLOW,
  343. onEnd: function () {
  344. //结束后回调
  345. },
  346. });
  347. }
  348. }
  349. //取区域内的随机图标
  350. function randomPoint() {
  351. var jd = haoutil.math.random(116.1 * 1000, 116.6 * 1000) / 1000;
  352. var wd = haoutil.math.random(30.8 * 1000, 31.1 * 1000) / 1000;
  353. var height = haoutil.math.random(1000, 9000);
  354. return new mars3d.LatLngPoint(jd, wd, height);
  355. }