HtmlWindow3.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. import {
  4. getGuid,
  5. } from "../common/common.js";
  6. import '../../Assets/styles/PopupWindow/HtmlWindow3.css';
  7. /**
  8. * 弹框内容,可放任何html
  9. */
  10. class HtmlWindow3 {
  11. /**
  12. * 初始化
  13. * @author joy/创睿
  14. * @param {Object} viewer 三维场景viewer对象
  15. * @param {Cesium.Cartesian3} position 坐标位置
  16. * @param {String} title 窗口标题
  17. * @param {String} html 弹框内容,可放任何html
  18. */
  19. constructor(viewer, position, title, html) {
  20. if (!viewer) throw new Cesium.DeveloperError('no viewer object!');
  21. if (!position) throw new Cesium.DeveloperError('no position object!');
  22. this.viewer = viewer; //弹窗创建的viewer
  23. this.position = position; //弹窗挂载的位置
  24. //弹窗挂载的位置
  25. if (position instanceof Cesium.Cartesian3) {
  26. this.position = position;
  27. } else {
  28. this.position = Cesium.Cartesian3.fromDegrees(position[0], position[1], position[2] || 0);
  29. }
  30. //如果有div就移除
  31. if (document.getElementsByClassName("popup3").length > 0) {
  32. document.getElementsByClassName("popup3")[0].remove();
  33. }
  34. this.id = "popup_" + getGuid();
  35. this.popupDiv = document.createElement("div");
  36. this.popupDiv.classList.add("popup3");
  37. this.popupDiv.id = this.id;
  38. // 将字符串模板生成的内容添加到DOM上
  39. this.viewer.container.append(this.popupDiv);
  40. // this.viewer.cesiumWidget.container.appendChild(this.popupDiv);
  41. //创建Html
  42. this.popupDiv.innerHTML = this._createHtml(title, html);
  43. //添加场景事件,实时更新窗口的位置
  44. this.viewer.scene.postRender.addEventListener(this.postRender, this);
  45. //绑定关闭事件
  46. document.getElementsByClassName("popup3-close-button")[0].onclick = () => {
  47. this.close();
  48. };
  49. }
  50. /**
  51. * 场景渲染事件 实时更新窗口的位置 使其与笛卡尔坐标一致
  52. * @ignore
  53. */
  54. postRender() {
  55. const canvasHeight = this.viewer.scene.canvas.height;
  56. const windowPosition = new Cesium.Cartesian2();
  57. Cesium.SceneTransforms.wgs84ToWindowCoordinates(
  58. this.viewer.scene,
  59. this.position,
  60. windowPosition
  61. );
  62. this.popupDiv.style.left = windowPosition.x + 70 + "px";
  63. this.popupDiv.style.top = windowPosition.y - this.popupDiv.offsetHeight - 20 + "px";
  64. const camerPosition = this.viewer.camera.position;
  65. let height = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(camerPosition).height;
  66. height += this.viewer.scene.globe.ellipsoid.maximumRadius;
  67. if ((!(Cesium.Cartesian3.distance(camerPosition, this.position) > height)) && this.viewer.camera.positionCartographic.height < 50000000) {
  68. this.popupDiv.style.display = "block";
  69. } else {
  70. this.popupDiv.style.display = "none";
  71. }
  72. }
  73. /**
  74. * 创建Html
  75. * @ignore
  76. * @param {Object} title
  77. * @param {Object} content
  78. */
  79. _createHtml(title, content) {
  80. let html = `
  81. <div class="leftLine-1"></div>
  82. <div class="leftLine-2"></div>
  83. <div class="popup-header">
  84. <span class="popup-title" >${title}</span>
  85. <span class="popup3-close-button" title="关闭" >×</span>
  86. </div>
  87. <div class="popup-content">
  88. ${content}
  89. </div>
  90. `
  91. return html;
  92. }
  93. }
  94. /**
  95. * 通用对外公开函数
  96. */
  97. Object.assign(HtmlWindow3.prototype, /** @lends HtmlWindow3.prototype */ {
  98. /**
  99. * 关闭按钮
  100. */
  101. close() {
  102. this.popupDiv.remove();
  103. this.viewer.scene.postRender.removeEventListener(this.postRender, this);
  104. }
  105. });
  106. export default HtmlWindow3;