Cesium|xt3d 军事标绘绘制编辑

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: 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('polygon')"></button>
        <button onclick="drawActivate('circle')">正圆</button>
        <button onclick="drawActivate('ellipse')">椭圆</button>
        <button onclick="drawActivate('sector')">扇形</button>
        <button onclick="drawActivate('rectangle')">矩形</button>
        <button onclick="drawActivate('closedcurve')">曲线面</button>
        <button onclick="drawActivate('finearrow')">直箭头</button>
        <button onclick="drawActivate('assaultdirection')">突击方向</button>
        <button onclick="drawActivate('attackarrow')">进攻方向</button>
        <button onclick="drawActivate('tailedattackarrow')">进攻方向尾</button>
        <button onclick="drawActivate('gatheringplace')">集结地</button>
        <button onclick="drawActivate('doublearrow')">钳击</button>
        <button onclick="drawActivate('squadcombat')">分队战斗</button>
        <button onclick="drawActivate('tailedsquadcombat')">分队战斗尾</button>
        <div>
            <button onclick="clearDraw()">清空</button>
            <button onclick="save()">保存</button>
            <div class="tip-item">绘制完成后点击“保存”按钮将绘制的内容保存到json文件(支持从json文件加载标绘内容)</div>
        </div>
    </div>
    <script>
        let xt3dInit = {
            init(el) {
                this.initViewer(el);
                this.load3dtiles();
                this.initPlot();
                this.initDatas();
            },

            //初始化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;
            },

            //初始化标绘
            initPlot() {
                this.militaryPlotLayer = new xt3d.LabelPlotting.MilitaryPlot.PlotLayer(this.viewer);
                this.militaryPlotLayer.setPlotSelectable(true);

                this.plotDraw = new xt3d.LabelPlotting.MilitaryPlot.PlotDraw(this.viewer);
                this.plotDraw.PlotDrawEndEvent.addEventListener((drawPlot, plotType) => {
                    drawPlot.remove();
                    this.militaryPlotLayer.addPlot(drawPlot.toGeoJson());
                });

                this.plotEdit = new xt3d.LabelPlotting.MilitaryPlot.PlotEdit(this.viewer, this.militaryPlotLayer);
                this.plotEdit.activate();

                this.plotEdit.PlotEditEndEvent.addEventListener(editPlot => {
                    console.log(editPlot); //编辑结束
                    console.log("编辑结束");
                });
            },

            //激活绘制
            drawActivate(plotType) {
                this.plotDraw.activate(plotType);
            },

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

            //保存数据
            savePlots() {
                const features = [];
                this.militaryPlotLayer.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.militaryPlotLayer.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 在线预览地址

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用Cesium API中的MilitaryPlotting扩展实现军事标绘代码示例: ```javascript var viewer = new Cesium.Viewer('cesiumContainer'); // 添加MilitaryPlotting扩展 viewer.extend(Cesium.MilitaryPlotting); // 创建一个标绘对象 var entity = viewer.entities.add({ position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883), polyline: { positions: Cesium.Cartesian3.fromDegreesArrayHeights([ -75.59777, 40.03883, 0, -75.59777, 40.13883, 0, -75.69777, 40.13883, 0, -75.69777, 40.03883, 0, -75.59777, 40.03883, 0 ]), width: 5, material: new Cesium.PolylineDashMaterialProperty({ color: Cesium.Color.fromCssColorString('#FF0000').withAlpha(0.7) }) }, // 添加军事标绘图形 military: { symbolCode: 'GFGPGLB----K---', // 任务图形代码 fillMaterial: new Cesium.ColorMaterialProperty( Cesium.Color.fromCssColorString('#FF0000').withAlpha(0.3) ), outlineColor: new Cesium.ColorMaterialProperty( Cesium.Color.fromCssColorString('#FF0000').withAlpha(0.7) ), // 边界线颜色 size: 50, // 图形大小 rotation: Cesium.Math.toRadians(45) // 旋转角度 } }); // 调整相机视角 viewer.zoomTo(entity); ``` 在上述代码中,我们首先创建了一个Cesium Viewer对象,并添加了MilitaryPlotting扩展。然后,我们创建了一个实体对象,并在其中添加了一个Polyline对象作为参考线,以及一个MilitaryPlotting对象作为军事标绘图形。最后,我们调用zoomTo函数调整相机视角,以便能够完整地显示标绘图形。 需要注意的是,上述代码仅仅是演示了如何使用MilitaryPlotting扩展进行军事标绘,实际应用中需要根据具体需求进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xt3d

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

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

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

打赏作者

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

抵扣说明:

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

余额充值