Cesium|xt3d 点线面绘制编辑

该博客演示了如何使用Cesium库与xt3d扩展库在Web环境中进行3D点、线、面的绘制与编辑。通过预览代码,可以了解到如何初始化Cesium Viewer,激活绘制工具,以及添加不同类型的绘制结果。同时,提供了清除所有绘制的功能。博客还包含了交互式的在线预览链接。
摘要由CSDN通过智能技术生成

Cesium|xt3d 点线面绘制编辑

效果

在这里插入图片描述

代码

 <!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>cesium|xt3d</title>
    <!-- 引入Cesium -->
    <script src="https://unpkg.com/cesium@1.84.0/Build/Cesium/Cesium.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/cesium@1.84.0/Build/Cesium/Widgets/widgets.css">

    <script src='http://www.xt3d.cn/libs/turf.min.js'></script>

    <!--  引入xt3d -->
    <script src="http://www.xt3d.cn/xt3dlib/xt3d.min.js"></script>
    <style>
        html,
        body,
        #map3d {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
        }
        
        .btn-container {
            position: absolute;
            left: 10px;
            top: 90px;
            padding: 10px 15px;
            border-radius: 4px;
            border: 1px solid rgba(128, 128, 128, 0.5);
            color: #ffffff;
            background: rgba(0, 0, 0, 0.4);
            box-shadow: 0 3px 14px rgb(128 128 128 / 50%);
            max-width: 300px;
        }
        
        button {
            background: transparent;
            border: 1px solid #00d0ffb8;
            color: white;
            padding: 7px 9px;
            border-radius: 2px;
            margin: 3px;
            cursor: pointer
        }
        
        .tip-item {
            margin: 2px 0px;
            padding: 5px 1px;
        }
    </style>
</head>

<body>
    <div id="map3d"></div>
    <div class="btn-container">
        <button onclick="drawActivate('Point')">绘制点</button>
        <button onclick="drawActivate('Polyline')">绘制线</button>
        <button onclick="drawActivate('Polygon')">绘制面</button>
        <button onclick="clearDraw()">清空绘制</button>

        <div class="tip-item">点击“绘制点”按钮后在场景中点击鼠标左键绘制点</div>
        <div class="tip-item">点击“绘制线”按钮后在场景中点击鼠标左键绘制线,点击鼠标右键结束绘制。</div>
        <div class="tip-item">点击“绘制面”按钮后在场景中点击鼠标左键绘制面,点击鼠标右键结束绘制。</div>
        <div class="tip-item">点击“清空绘制”按钮后删除所有绘制对象</div>
        <div class="tip-item">第一次绘制点后可能图标加载较慢,属正常情况,绘制结束会有回调事件,在回调事件中获取绘制结果</div>
    </div>
    </div>
    <script>
        let xt3dInit = {
            init(el) {
                this.initViewer(el);
                this.load3dtiles();
                this.initDrawEdit();
            },

            //初始化viewer
            initViewer(el) {
                this.viewer = new Cesium.Viewer(el, {
                    infoBox: false,
                    selectionIndicator: false,
                    navigation: false,
                    animation: false,
                    shouldAnimate: false,
                    timeline: false,
                    baseLayerPicker: false,
                    geocoder: false,
                    homeButton: false,
                    sceneModePicker: false,
                    navigationHelpButton: false,
                    imageryProvider: new Cesium.UrlTemplateImageryProvider({
                        url: "https://t7.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=tdtTk"
                    })
                });
                this.viewer.scene.globe.depthTestAgainstTerrain = true; //默认为false 
            },

            //初始化绘制编辑
            initDrawEdit() {
                this.edit = new xt3d.LabelPlotting.EntityEdit(this.viewer);
                this.edit.activate();
                this.edit.EditEndEvent.addEventListener((result) => {
                    console.log(result)
                })


                this.drawTool = new xt3d.LabelPlotting.EntityDraw(this.viewer);
                this.drawTool.DrawEndEvent.addEventListener((result, positions, drawType) => {
                    result.remove();
                    this.addDrawResult(positions, drawType);
                })
            },

            //添加绘制结果
            addDrawResult(positions, drawType) {
                switch (drawType) {
                    case "Point":
                        this.generatePoint(positions);
                        break;
                    case "Polyline":
                        this.generatePolyline(positions);
                        break;
                    case "Polygon":
                        this.generatePolygon(positions);
                        break;
                }
            },

            generatePoint(positions) {
                let entity = this.viewer.entities.add({
                    Type: "EditableMarker",
                    position: positions[0],
                    billboard: {
                        image: "/data.xt3d.cn/assets/images/poi/sp.png",
                        scaleByDistance: new Cesium.NearFarScalar(300, 1, 1200, 0.4), //设置随图缩放距离和比例
                        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 10000), //设置可见距离 10000米可见
                        verticalOrigin: Cesium.VerticalOrigin.BOTTOM
                    }
                })
            },

            generatePolyline(positions) {
                let entity = this.viewer.entities.add({
                    Type: "EditablePolyline",
                    polyline: {
                        positions: positions,
                        width: 2,
                        material: new Cesium.PolylineDashMaterialProperty({
                            color: Cesium.Color.YELLOW,
                        }),
                        depthFailMaterial: new Cesium.PolylineDashMaterialProperty({
                            color: Cesium.Color.YELLOW,
                        }),
                    }
                })
            },

            generatePolygon(positions) {
                let entity = this.viewer.entities.add({
                    Type: "EditablePolygon",
                    polygon: {
                        hierarchy: positions,
                        material: Cesium.Color.RED.withAlpha(0.4),
                        perPositionHeight: true
                    },
                    polyline: {
                        positions: positions.concat(positions[0]),
                        width: 1,
                        material: new Cesium.PolylineDashMaterialProperty({
                            color: Cesium.Color.YELLOW,
                        })
                    }
                })
            },

            //激活绘制工具
            drawActivate(type) { //type in Point Polyline Polygon
                this.drawTool.activate(type);
            },

            //清空所有绘制
            clearDraw() {
                this.viewer.entities.removeAll();
            },

            //加载三维模型
            load3dtiles() {
                var tileset = this.viewer.scene.primitives.add(
                    new Cesium.Cesium3DTileset({
                        url: "http://www.xt3d.cn/data/offset_3dtiles/tileset.json",
                    })
                );

                tileset.readyPromise
                    .then(tileset => {
                        this.viewer.zoomTo(
                            tileset,
                        );
                        xt3d.TilesetPlugin.setTilesetHeight(tileset, 55);
                    })
                    .otherwise(function(error) {
                        console.log(error);
                    });
            },


            destroy() {
                this.viewer.entities.removeAll();
                this.viewer.imageryLayers.removeAll(true);
                this.viewer.destroy();
            }
        }

        xt3dInit.init("map3d");

        function drawActivate(type) {
            xt3dInit.drawActivate(type);
        }

        function clearDraw() {
            xt3dInit.clearDraw();
        }
    </script>
</body>

</html>

预览地址

xt3d 在线预览地址

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xt3d

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

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

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

打赏作者

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

抵扣说明:

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

余额充值