/* 引入Cesium */ // import * as Cesium from 'Cesium'; import { getGuid, } from "../common/common.js"; import '../../Assets/styles/PopupWindow/HtmlWindow3.css'; /** * 弹框内容,可放任何html */ class HtmlWindow3 { /** * 初始化 * @author joy/创睿 * @param {Object} viewer 三维场景viewer对象 * @param {Cesium.Cartesian3} position 坐标位置 * @param {String} title 窗口标题 * @param {String} html 弹框内容,可放任何html */ constructor(viewer, position, title, html) { if (!viewer) throw new Cesium.DeveloperError('no viewer object!'); if (!position) throw new Cesium.DeveloperError('no position object!'); this.viewer = viewer; //弹窗创建的viewer this.position = position; //弹窗挂载的位置 //弹窗挂载的位置 if (position instanceof Cesium.Cartesian3) { this.position = position; } else { this.position = Cesium.Cartesian3.fromDegrees(position[0], position[1], position[2] || 0); } //如果有div就移除 if (document.getElementsByClassName("popup3").length > 0) { document.getElementsByClassName("popup3")[0].remove(); } this.id = "popup_" + getGuid(); this.popupDiv = document.createElement("div"); this.popupDiv.classList.add("popup3"); this.popupDiv.id = this.id; // 将字符串模板生成的内容添加到DOM上 this.viewer.container.append(this.popupDiv); // this.viewer.cesiumWidget.container.appendChild(this.popupDiv); //创建Html this.popupDiv.innerHTML = this._createHtml(title, html); //添加场景事件,实时更新窗口的位置 this.viewer.scene.postRender.addEventListener(this.postRender, this); //绑定关闭事件 document.getElementsByClassName("popup3-close-button")[0].onclick = () => { this.close(); }; } /** * 场景渲染事件 实时更新窗口的位置 使其与笛卡尔坐标一致 * @ignore */ postRender() { const canvasHeight = this.viewer.scene.canvas.height; const windowPosition = new Cesium.Cartesian2(); Cesium.SceneTransforms.wgs84ToWindowCoordinates( this.viewer.scene, this.position, windowPosition ); this.popupDiv.style.left = windowPosition.x + 70 + "px"; this.popupDiv.style.top = windowPosition.y - this.popupDiv.offsetHeight - 20 + "px"; const camerPosition = this.viewer.camera.position; let height = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(camerPosition).height; height += this.viewer.scene.globe.ellipsoid.maximumRadius; if ((!(Cesium.Cartesian3.distance(camerPosition, this.position) > height)) && this.viewer.camera.positionCartographic.height < 50000000) { this.popupDiv.style.display = "block"; } else { this.popupDiv.style.display = "none"; } } /** * 创建Html * @ignore * @param {Object} title * @param {Object} content */ _createHtml(title, content) { let html = `