Cesium教程(9)---编辑Entity图形控制点、拖拽Entity移动、删除Entity

本学习系列将以Cesium + Vue3 +Typescript+elementplus作为主要技术栈展开,后续会循序渐进,持续探索Cesium的高级功能,相关源码全部免费公开。详情请查看原文 Cesium教程9--编辑Entity图形控制点、拖拽Entity移动、删除Entity

上篇主要介绍如何使用Cesium标绘简单的点、线以及多边形。本篇在上篇的基础上增加图形编辑功能,比如拖动图形控制点、移动图形位置、删除图形等。先看效果 主要代码

Typescript 复制代码
import {
    Entity, Cartesian3, defined, ScreenSpaceEventHandler,
    ScreenSpaceEventType, Viewer
} from 'cesium';
import { EditHelper } from './EditHelper';
import EventDispatcher from '../EventDispatcher/EventDispatcher';
export class MultiEditManager {
    private viewer: Viewer;
    private handler: ScreenSpaceEventHandler;
    private activeEditor?: { entity: Entity; helper: EditHelper };
    private static instance: MultiEditManager;
    private dispatcher: EventDispatcher;
    constructor(viewer: Viewer,dispatcher: EventDispatcher) {
        this.viewer = viewer;
        this.handler = new ScreenSpaceEventHandler(viewer.canvas);
        this.bindClick();
        this.bindKeyDelete();
        this.dispatcher = dispatcher;
    }
    /** 绑定Delete键删除当前编辑entity */
    private bindKeyDelete() {
        window.addEventListener('keydown', (e) => {
            if (e.key === 'Delete' && this.activeEditor?.entity) {
                this.viewer.entities.remove(this.activeEditor.entity);                
                this.clear();
                this.dispatcher.dispatchEvent('EDITEND',{text:""});
            }
        });
    }
    // 获取单例实例
    public static getInstance(viewer?: Viewer, dispatcher?: EventDispatcher): MultiEditManager {
        if (!MultiEditManager.instance) {
            if (!viewer) throw new Error("MultiEditManager需要传入viewer实例");
            if (!dispatcher) throw new Error("MultiEditManager需要传入dispatcher实例");
            MultiEditManager.instance = new MultiEditManager(viewer, dispatcher);
        }
        return MultiEditManager.instance;
    }
    /** 把画好的 entity 登记进来(由外部调用) */
    register(entity: Entity, positions: Cartesian3[]) {
        (entity as any)._editPositions = positions.map(p => p.clone());
    }
    /** 销毁管理器 */
    destroy() {
        this.clear();
        this.handler.destroy();
    }
    /** 绑定单击 */
    private bindClick() {
        this.handler.setInputAction((e: any) => this.onClick(e), ScreenSpaceEventType.LEFT_CLICK);
    }
    /** 单击逻辑 */
    private onClick(e: any) {
        const picked = this.viewer.scene.pick(e.position);
        if (defined(picked) && defined(picked.id) && (picked.id as any)._editPositions) {
            // 点到了可编辑 entity
            const entity = picked.id as Entity;         
         
            if (this.activeEditor?.entity === entity) return; // 已经是它,忽略
            this.clear();               // 先关掉之前的
            this.startEdit(entity);     // 打开新的           
            this.dispatcher.dispatchEvent('EDITSTART',{text:"编辑开始,可拖动控制点编辑、可拖动图形移动位置,delete键删除当前图形;点击空白,结束编辑;"});
        } else {
            // 点到空白
            this.clear();
            this.dispatcher.dispatchEvent('EDITEND',{text:""});
        }
    }
    /** 启动某个 entity 的编辑 */
    private startEdit(entity: Entity) {
        const positions: Cartesian3[] = (entity as any)._editPositions;
        const helper = new EditHelper(this.viewer, entity, positions, (newP) => {           
            (entity as any)._editPositions = newP; // 其实引用没变,只是兜底
        });
        helper.start();
        this.activeEditor = { entity, helper };
    }
    /** 关闭当前编辑 */
    private clear() {
        if (!this.activeEditor) return;
        this.activeEditor.helper.destroy();
        this.activeEditor = undefined;
    }
}

更多源码请查看原文 Cesium教程9--编辑Entity图形控制点、拖拽Entity移动、删除Entity

相关推荐
时间的情敌3 天前
Vue3 + Cesium 实战:本地上传 GLB 模型、模型上叠加 GLB、点击控制透明度
前端·vue.js·cesium
LuHang4 天前
从天地图切换到高德地图:一次真实的底图迁移实战
前端·vue.js·cesium
总要学点什么4 天前
第92篇:三维特效-圆(逐渐消逝)——从圆心开始,让一个洞把整片光啃到只剩外圈
gis·webgl·cesium·空间分析·1024程序员节
总要学点什么5 天前
第89篇:三维特效-螺旋圆效果——让螺旋臂卷起一圈圈外扩的涟漪
开发语言·javascript·ecmascript·webgl·cesium·空间分析·1024程序员节
vjmap5 天前
Cesium + MCP:在 Cursor、Claude Code 里一句话生成地图视频,唯杰 AI 地图动画上线
gis·cesium·mcp
GoldenSpider.AI5 天前
一张户型图做成浏览器 3D 漫游:house.json 统一模型、1.2 m 剖切图纸与分阶段验收
ai编程·webgl·three.js·3d漫游·claude code
Jmyd01238 天前
WebGL 三维党史馆怎么扛住 720 度全景与移动端弱网?
webgl·三维数字化
总要学点什么8 天前
第81篇:数据可视化-PM2.5分布——让圆点的胖瘦替每座城市报出浓度
前端·信息可视化·echarts·cesium·空间分析
limit007510 天前
三维 WebGIS 工具选型:加载、样式、分析、交付,一套界面能做完什么
android·前端·web3·安卓·webgl
总要学点什么11 天前
第79篇:数据可视化-模拟迁徙效果——把三十条航线画成会飞的弧
3d·信息可视化·数据挖掘·数据分析·gis·webgl·cesium