HtmlWindow.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. import {
  4. getGuid,
  5. } from "../common/common.js";
  6. import '../../Assets/styles/PopupWindow/HtmlWindow1.css';
  7. /**
  8. * 弹框内容,可放任何html
  9. */
  10. class HtmlWindow {
  11. /**
  12. * 初始化
  13. * @author joy/创睿
  14. * @param {Object} viewer 三维场景viewer对象
  15. * @param {Cesium.Cartesian3} position 坐标位置
  16. * @param {String} title 窗口标题
  17. * @param {Object} html 弹框内容,可放任何html
  18. * @param {Number}offsetHeight 底部偏移高度,弹窗底部偏移高度需要设置一个合适的值,否则很容易导致场景拖动的时候,弹窗存在偏移
  19. */
  20. constructor(viewer, position, title, html, offsetHeight) {
  21. if (!viewer) throw new Cesium.DeveloperError('no viewer object!');
  22. if (!position) throw new Cesium.DeveloperError('no position object!');
  23. let _self = this;
  24. this.viewer = viewer; //弹窗创建的viewer
  25. this.offsetHeight = offsetHeight;
  26. //弹窗挂载的位置
  27. if (position instanceof Cesium.Cartesian3) {
  28. this.position = position;
  29. } else {
  30. this.position = Cesium.Cartesian3.fromDegrees(position[0], position[1], position[2] || 0);
  31. }
  32. //如果有div就移除
  33. if (document.getElementsByClassName("popup1").length > 0) {
  34. document.getElementsByClassName("popup1")[0].remove();
  35. viewer.entities.remove(viewer.entities.getById("popupPoint"));
  36. }
  37. this.id = "popup_" + getGuid();
  38. this.popupDiv = document.createElement("div");
  39. this.popupDiv.classList.add("popup1");
  40. this.popupDiv.id = this.id;
  41. // 将字符串模板生成的内容添加到DOM上
  42. this.viewer.container.append(this.popupDiv);
  43. // this.viewer.cesiumWidget.container.appendChild(this.popupDiv);
  44. //创建Html
  45. this.popupDiv.innerHTML = this._createHtml(title, html);
  46. //添加场景事件,实时更新窗口的位置
  47. this.viewer.scene.postRender.addEventListener(this.postRender, this);
  48. this.initPoint();
  49. //绑定关闭事件
  50. document.getElementsByClassName("popup1-close-button")[0].onclick = () => {
  51. this.close();
  52. };
  53. }
  54. /**
  55. * 场景渲染事件 实时更新窗口的位置 使其与笛卡尔坐标一致
  56. * @ignore
  57. */
  58. postRender() {
  59. const canvasHeight = this.viewer.scene.canvas.height;
  60. const windowPosition = new Cesium.Cartesian2();
  61. Cesium.SceneTransforms.wgs84ToWindowCoordinates(
  62. this.viewer.scene,
  63. this.position,
  64. windowPosition
  65. );
  66. let elWidth = this.popupDiv.offsetWidth;
  67. let elHeight = this.popupDiv.offsetHeight;
  68. //elHeight 值需要设置一个合适的,否则很容易导致场景拖动的时候,弹窗存在偏移
  69. if (!!this.offsetHeight) {
  70. elHeight += this.offsetHeight;
  71. }
  72. this.popupDiv.style.left = windowPosition.x - elWidth / 2 + "px";
  73. this.popupDiv.style.top = windowPosition.y - elHeight + "px";
  74. const camerPosition = this.viewer.camera.position;
  75. let height = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(camerPosition).height;
  76. height += this.viewer.scene.globe.ellipsoid.maximumRadius;
  77. if ((!(Cesium.Cartesian3.distance(camerPosition, this.position) > height)) && this.viewer.camera.positionCartographic.height < 50000000) {
  78. this.popupDiv.style.display = "block";
  79. } else {
  80. this.popupDiv.style.display = "none";
  81. }
  82. }
  83. /**
  84. * 创建Html
  85. * @ignore
  86. * @param {Object} header
  87. * @param {Object} content
  88. */
  89. _createHtml(header, content) {
  90. let html = `
  91. <div class="popup-header">
  92. ${header }
  93. <span class="popup1-close-button">×</span>
  94. </div>
  95. <div class="popup-content">
  96. ${content}
  97. </div>
  98. <div class="popup-tip">
  99. </div>
  100. `
  101. return html;
  102. }
  103. /**
  104. * 用于调试弹窗是否对得上位置,弹窗对上了该点,不然很容易出现弹窗偏移的问题
  105. * @ignore
  106. */
  107. initPoint() {
  108. this.billboard = this.viewer.entities.add({
  109. id: "popupPoint",
  110. name: "popupPoint",
  111. position: this.position, // 点的经纬度坐标
  112. billboard: {
  113. image: 'jt3dSDK/imgs/point/point.png',
  114. horizontalOrigin: Cesium.HorizontalOrigin.center,
  115. verticalOrigin: Cesium.VerticalOrigin.bottom,
  116. scale: 1,
  117. pixelOffset: new Cesium.Cartesian2(0, 0),
  118. disableDepthTestDistance: Number.POSITIVE_INFINITY,
  119. },
  120. });
  121. }
  122. }
  123. /**
  124. * 通用对外公开函数
  125. */
  126. Object.assign(HtmlWindow.prototype, /** @lends HtmlWindow.prototype */ {
  127. /**
  128. * 关闭按钮
  129. */
  130. close() {
  131. this.popupDiv.remove();
  132. this.viewer.scene.postRender.removeEventListener(this.postRender, this);
  133. this.viewer.entities.remove(this.billboard);
  134. }
  135. });
  136. export default HtmlWindow;