123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- /* 引入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 = `
- <div class="leftLine-1"></div>
- <div class="leftLine-2"></div>
- <div class="popup-header">
- <span class="popup-title" >${title}</span>
- <span class="popup3-close-button" title="关闭" >×</span>
- </div>
- <div class="popup-content">
- ${content}
- </div>
- `
- return html;
- }
- }
- /**
- * 通用对外公开函数
- */
- Object.assign(HtmlWindow3.prototype, /** @lends HtmlWindow3.prototype */ {
- /**
- * 关闭按钮
- */
- close() {
- this.popupDiv.remove();
- this.viewer.scene.postRender.removeEventListener(this.postRender, this);
- }
- });
- export default HtmlWindow3;
|