便利贴--10{Cesium+js随地图弹窗}

本文介绍如何使用Cesium+js实现地图上的动态弹窗功能。通过构造函数定义弹窗样式及位置更新逻辑,确保随着地图视角变化而调整弹窗位置。

使用

// 定制化窗体
      const popupsDom = new global.DC.mobileDivForms(state.mviewer, {
   
   
        domId: "mobilePopup",
        title: data.query.name || "成教楼  ",
        className: "mobilePopup",
        content: document.getElementById("mobile-map_content_content"),
        position: [data.position],
      });

构造函数主体


import DC from '@dvgis/dc-sdk/dist/dc.base.min' // 基础包
import DcCore from '@dvgis/dc-sdk/dist/dc.core.min' // 核心包

var $ = window.$

DC.use(DcCore) // 安装DC核心库

class mobileDivForms {
   
   
    /**
     * @popup 存放 dom 相关
     *
     */
    constructor(viewer, popup) {
   
   
        this._viewer = viewer;
        this._popup = popup;
        this._position = popup.position;
        this._title = popup.title;
        this._content = popup.content;
        this.appendPopup();
        this.isOpen = true;
    }

    get viewer() {
   
   
        return this._viewer
    }

    get title() {
   
   
        return this._title
    }

    get content() {
   
   
        return this._content
    }

    get popup() {
   
   
        return this._popup
    }

    get position() {
   
   
        return this._position
    }

    appendPopup() {
   
   
        /**
         * @domId 存放 dom 的id, 多个数组,单个字符串
         */

        this.createVideoWindowAll(this._popup.domId, this._popup.className, this._position[0])
    }

    // 创建元素并追加
    createVideoWindowAll(id, className, position) {
   
   
        let dom = document.getElementById('mobile-map_popup_content');
        dom.style.display = 'block';

        var childs = document.getElementById('mobile-map_popup_content').getElementsByTagName('div')

        if (childs.length > 0) {
   
   
            for (var i = 0; i < childs.length; i++) {
   
   
                document.getElementById('mobile-map_popup_content').removeChild(childs[i])
            }
        }

        var self = this

        var el = $("<div class=''></div>")

        // var titleDiv = $("<div class='mobiletitle'></div>").text(this._title)
        // var labelContent = $("<div class='mobilelabel-content'></div>").append(this._content)

        // el.append(titleDiv)
        // el.append(labelContent)
        el.append(this._content)

        el.attr('id', id)

        el.addClass(className || '')

        $('#mobile-map_popup_content').append(el)
        // let time;
        // function cons(e) {
   
   
        //     time = setTimeout(() => {
   
   
        //         console.log(e)
        //         console.log(e.sun._drawingBufferHeight)
        //         console.log("roll: ", e.camera.roll)
        //         console.log("pitch: ", e.camera.pitch)
        //         console.log("heading: ", e.camera.heading)
        //         clearTimeout(time);
        //         time = "";
        //     }, 2000);
        // }

        this._viewer.scene.postRender.addEventListener(function (e) {
   
   
            if (!self.isOpen) {
   
   
                return;
            }
            const windowCoord = DC.Namespace.Cesium.SceneTransforms.wgs84ToWindowCoordinates(
                self._viewer.scene,
                position
            )
            // time || cons(e);
            self.positionPopUp(windowCoord, id)
        })
    }

    positionPopUp(windowCoord, id) {
   
   
        if (!windowCoord) {
   
   
            return;
        }
        // console.log(windowCoord)
        const wx = document.getElementById('app').clientWidth;
        const x = windowCoord.x - (wx / 100 * 35);
        const y = windowCoord.y - document.getElementById(id).offsetHeight - 15;
        // x = windowCoord.x - document.getElementById(id).offsetWidth

        // document.getElementById(id).style.cssText = `
        document.getElementById(id).style.cssText = `
        visibility:visible;
        z-index:98;
        transform:translate3d(${
     
     Math.round(x)}px,${
     
     Math.round(y)}px, 0);
        `
    }
    closeOur() {
   
   
        this.isOpen = false;
        let dom = document.getElementById('mobile-map_popup_content');
        dom.style.display = 'none';
        // var childs = document.getElementById('mobile-map_popup_content').getElementsByTagName('div')

        // if (childs.length > 0) {
   
   
        //     for (var i = 0; i < childs.length; i++) {
   
   
        //         document.getElementById('mobile-map_popup_content').removeChild(childs[i])
        //     }
        // }
    }
}

export default mobileDivForms

html展示页面–可查看上一篇–便利贴9有一部分

<template>
  <div
    ref="mobile-mapContent
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻动琴弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值