Cesium|xt3d gltf标绘绘制编辑

Cesium|xt3d gltf标绘绘制编辑

效果

在这里插入图片描述

代码

 <!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: 380px;
        }
        
        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('fireMan')">消防员</button>
        <button onclick="drawActivate('fireCar')">消防车</button>
        <button onclick="drawActivate('police')">警察</button>
        <button onclick="drawActivate('policeCar')">警车</button>
        <button onclick="clearDraw()">清空</button>
        <button onclick="save()">保存</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 class="tip-item">点击选中模型,选中后鼠标按住移动。</div>
    </div>
    </div>
    <script>
        let xt3dInit = {
            init(el) {
                this.initViewer(el);
                this.initGltfDraw();
                this.initGltfEdit();
                this.load3dtiles();
                this.initDatas();
            },

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

            //加载数据
            initDatas() {
                fetch("/data.xt3d.cn/assets/data/labelplotting/1602600681304.json").then(res => {
                    return res.json();
                }).then(res => {
                    let features = res.features;
                    features.forEach(feature => {
                        this.gltfPlotLayer.addPlot(feature);
                    })
                }).catch(err => {
                    console.log(err)
                })
            },

            //初始化绘制
            initGltfDraw() {
                this.gltfPlotLayer = new xt3d.GltfModel.GltfPlot.PlotLayer(this.viewer);
                this.gltfPlotLayer.setPlotSelectable(true);

                this.gltfDraw = new xt3d.GltfModel.GltfPlot.PlotDraw(this.viewer);
                this.gltfDraw.DrawEndEvent.addEventListener((position) => {
                    this.handleDrawEnd(position);
                })
            },

            //初始化编辑
            initGltfEdit() {
                this.gltfPlotEdit = new xt3d.GltfModel.GltfPlot.PlotEdit(
                    this.viewer,
                    this.gltfPlotLayer
                );
            },

            //绘制结束事件
            handleDrawEnd(position) {
                switch (this.drawPlotType) {
                    case "police":
                        this.addPolice(position);
                        break;
                    case "policeCar":
                        this.addPoliceCar(position);
                        break;
                    case "fireMan":
                        this.addFireMan(position);
                        break;
                    case "fireCar":
                        this.addFireCar(position);
                        break;
                }
            },

            addPolice(position) {
                var geoFeature = {
                    type: "Feature",
                    properties: {
                        plotCode: xt3d.GltfModel.getPlotCode(),
                        modelUrl: "/data.xt3d.cn/assets/gltf/police.gltf",
                        style: {
                            width: 1,
                            height: 1.6,
                            scale: 2,
                            heading: 0
                        },
                        attr: {}
                    },
                    geometry: {
                        type: "Point",
                        coordinates: xt3d.GltfModel.cartesian3ToCoordinates(position)
                    }
                };
                this.gltfPlotLayer.addPlot(geoFeature);
            },

            addPoliceCar(position) {
                var geoFeature = {
                    type: "Feature",
                    properties: {
                        plotCode: xt3d.GltfModel.getPlotCode(),
                        modelUrl: "/data.xt3d.cn/assets/gltf/police_car.gltf",
                        style: {
                            width: 1.4,
                            height: 1.8,
                            scale: 1.5,
                            heading: 0
                        },
                        attr: {}
                    },
                    geometry: {
                        type: "Point",
                        coordinates: xt3d.GltfModel.cartesian3ToCoordinates(position)
                    }
                };
                this.gltfPlotLayer.addPlot(geoFeature);
            },

            addFireMan(position) {
                var geoFeature = {
                    type: "Feature",
                    properties: {
                        plotCode: xt3d.GltfModel.getPlotCode(),
                        modelUrl: "/data.xt3d.cn/assets/glb/xiaofangyuan.glb",
                        style: {
                            width: 1,
                            height: 1.6,
                            scale: 1,
                            heading: 0
                        },
                        attr: {}
                    },
                    geometry: {
                        type: "Point",
                        coordinates: xt3d.GltfModel.cartesian3ToCoordinates(position)
                    }
                };
                this.gltfPlotLayer.addPlot(geoFeature);
            },

            addFireCar(position) {
                var geoFeature = {
                    type: "Feature",
                    properties: {
                        plotCode: xt3d.GltfModel.getPlotCode(),
                        modelUrl: "/data.xt3d.cn/assets/gltf/xiaofangche.gltf",
                        style: {
                            width: 1,
                            height: 1.6,
                            scale: 1,
                            heading: 0
                        },
                        attr: {}
                    },
                    geometry: {
                        type: "Point",
                        coordinates: xt3d.GltfModel.cartesian3ToCoordinates(position)
                    }
                };
                this.gltfPlotLayer.addPlot(geoFeature);
            },

            //激活绘制工具
            drawActivate(type) {
                this.drawPlotType = type;
                this.gltfDraw.activate();
            },

            //保存文件
            savePlots() {
                const features = [];
                this.gltfPlotLayer.plots.forEach(plot => {
                    features.push(plot.toGeoJson());
                })
                let geojson = {
                    "type": "FeatureCollection",
                    "features": features
                }
                let data = JSON.stringify(geojson);

                var blob = new Blob([data], {
                    type: 'text/json'
                });
                var e = document.createEvent('MouseEvents');
                var a = document.createElement('a');
                a.download = new Date().getTime() + ".json";
                a.href = window.URL.createObjectURL(blob);
                a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
                e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                a.dispatchEvent(e);
            },

            //清空
            clear() {
                this.gltfPlotLayer.clear();
            },

            //加载3dtiles数据
            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.clear();
        }

        function save() {
            xt3dInit.savePlots();
        }
    </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、付费专栏及课程。

余额充值