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://cdn.jsdelivr.net/npm/cesium@1.84.0/Build/Cesium/Cesium.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/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;
        }
    </style>
</head>

<body>
    <div id="map3d"></div>
    <script>
        let xt3dInit = {
            init(el) {
                this.initViewer(el);
                this.setView();
                this.colors = this.generateColors();
                this.labels = this.generateLabels();
                this.loadRegions();
            },

            //颜色
            generateColors() {
                return [
                    Cesium.Color.AQUA,
                    // Cesium.Color.fromCssColorString("rgb(15, 100, 157)"),
                    Cesium.Color.GREEN,
                    //Cesium.Color.fromCssColorString("rgb(0, 69, 154)"),
                    Cesium.Color.YELLOW,
                    //Cesium.Color.fromCssColorString("rgb(2, 165, 181)"),
                    Cesium.Color.RED,
                ]
            },

            //标签
            generateLabels() {
                return [{
                    position: Cesium.Cartesian3.fromDegrees(103.88545983932153, 36.033216960244246, 0),
                    name: "沿河区"
                }, {
                    position: Cesium.Cartesian3.fromDegrees(103.87214667212257, 36.038301982724796, 0),
                    name: "环城区"
                }, {
                    position: Cesium.Cartesian3.fromDegrees(103.86194888078703, 36.03969446525105, 0),
                    name: "封控区"
                }, {
                    position: Cesium.Cartesian3.fromDegrees(103.85091307483806, 36.041102519445715, 0),
                    name: "核心区"
                }]
            },

            //初始化viewer
            initViewer(el) {

                this.viewer = new Cesium.Viewer(el, {
                    infoBox: false,
                    selectionIndicator: false,
                    navigation: false,
                    animation: true,
                    timeline: false,
                    baseLayerPicker: false,
                    geocoder: false,
                    homeButton: false,
                    sceneModePicker: false,
                    navigationHelpButton: false,
                    shouldAnimate: true,
                    imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
                        url: 'http://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer'
                    })
                });
                this.viewer.scene.globe.depthTestAgainstTerrain = false; //默认为false  
                this.viewer.animation.container.style.visibility = 'hidden'; // 不显示动画控件 

                // 亮度设置
                let stages = this.viewer.scene.postProcessStages;
                this.viewer.scene.brightness = this.viewer.scene.brightness || stages.add(Cesium.PostProcessStageLibrary.createBrightnessStage());
                this.viewer.scene.brightness.enabled = true;
                this.viewer.scene.brightness.uniforms.brightness = 1.2;

                //是否开启抗锯齿
                this.viewer.scene.fxaa = true;
                this.viewer.scene.postProcessStages.fxaa.enabled = true;
            },

            //区域
            loadRegions() {
                fetch("/data.xt3d.cn/assets/data/arealabel.json").then(res => {
                    return res.json();
                }).then(res => {
                    let features = res.features;
                    features.forEach((item, index) => {
                        this.addRegion(item, index);
                    });
                })
            },

            addRegion(feature, index) {
                let degreesArrayHeights = this.getDegreesArrayHeights(feature);
                let positions = Cesium.Cartesian3.fromDegreesArrayHeights(degreesArrayHeights);
                let polygon = new Cesium.PolygonGeometry({
                    polygonHierarchy: new Cesium.PolygonHierarchy(positions),
                    vertexFormat: Cesium.VertexFormat.ALL
                });
                let geometry = Cesium.PolygonGeometry.createGeometry(polygon);
                let instance = new Cesium.GeometryInstance({
                    geometry: geometry
                });

                this.viewer.scene.primitives.add(new Cesium.Primitive({
                    geometryInstances: instance,
                    appearance: new xt3d.PolygonObject.PrimitiveGradientAppearance(this.colors[index]),
                }));

                this.addOutLine(positions, this.colors[index]);
                this.addLabel(index, this.colors[index]);
            },

            //添加边线
            addOutLine(positions, color) {
                this.viewer.entities.add({
                    polyline: {
                        positions: positions,
                        width: 2,
                        material: new Cesium.PolylineDashMaterialProperty({
                            color: color.withAlpha(1)
                        })
                    }
                })
            },

            //标签
            addLabel(index, color) {
                this.viewer.entities.add({
                    position: this.labels[index].position,
                    label: {
                        text: this.labels[index].name,
                        fillColor: color,
                        scale: 0.5,
                        font: 'normal 45px MicroSoft YaHei',
                        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 9000000),
                        scaleByDistance: new Cesium.NearFarScalar(50000, 1, 500000, 0.5),
                        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                        pixelOffset: new Cesium.Cartesian2(0, -10),
                        outlineWidth: 10,
                        outlineColor: Cesium.Color.BLACK
                    }
                })
            },

            //获取坐标串
            getDegreesArrayHeights(feature) {
                let degreesArrayHeights = [];
                let coordinates;
                if (feature.geometry.type == "MultiPolygon") {
                    coordinates = feature.geometry.coordinates[0][0];
                } else if (feature.geometry.type == "Polygon") {
                    coordinates = feature.geometry.coordinates[0];
                }
                //坐标串转为需要的格式[x,y,z,x,y,z....]
                for (let i = 0; i < coordinates.length; i++) {
                    const element = coordinates[i];
                    degreesArrayHeights.push(element[0]);
                    degreesArrayHeights.push(element[1]);
                    degreesArrayHeights.push(100);
                }
                return degreesArrayHeights;
            },

            setView() {
                let flyToOpts = {
                    destination: {
                        x: -1238875.9606814845,
                        y: 5021694.589907374,
                        z: 3731291.006898492
                    },
                    orientation: {
                        heading: 6.2243565276080295,
                        pitch: -0.9203738754493909,
                        roll: 6.282874205266332
                    },
                    duration: 1
                };
                this.viewer.scene.camera.setView(flyToOpts);
            },

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

        xt3dInit.init("map3d");
    </script>
</body>

</html>

预览地址

xt3d 在线预览地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xt3d

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

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

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

打赏作者

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

抵扣说明:

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

余额充值