天天看点

Cesium|xt3d单体化分栋效果代码预览地址

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">

    <title>cesium|xt3d</title>
    <!-- 引入Cesium -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/Build/Cesium/Cesium.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/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;
        }
        
        .form-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;
        }
        
        .tip-item {
            margin: 2px 0px;
            padding: 5px 1px;
        }
    </style>
</head>

<body>
    <div id="map3d"></div>
    <div class="form-container">
        鼠标左键点击楼栋,查看楼栋信息。(部分房屋没有做单体化)
    </div>
    <script>
        let xt3dInit = {
            init(el) {
                this.initViewer(el);
                this.load3dtiles();
                this.initDTH();
                this.setView();
            },

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


            initDTH() {
                this.fdDTH = new xt3d.DTH.FD(this.viewer, {
                    fdDataServerBaseUrl: "http://106.53.103.200:8080/geoserver/xt3d/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=xt3d%3Abuild_offset&maxFeatures=50&outputFormat=application%2Fjson"
                });
                this.fdDTH.BuildingSelectedEvent.addEventListener((properties, position) => {
                    if (!properties) {
                        this.closeBuildInfo();
                    } else {
                        this.showBuildInfo(properties, position);
                    }
                })

                this.fdDTH.activate();
            },

            //显示建筑信息
            showBuildInfo(properties, position) {
                const fileds = ["小区名称", "楼栋名称", "单元数", "楼栋长名称", "楼栋长电话"];
                const values = [properties.communityN, properties.buildName, properties.unitCount + "(个)", properties.leaderName, properties.leaderPhon];
                this.buildInfoWindow = new xt3d.DTH.BuildInfoWindow(this.viewer, position, "楼栋信息", fileds, values);
            },

            closeBuildInfo() {
                if (this.buildInfoWindow) {
                    this.buildInfoWindow.close();
                    this.buildInfoWindow = undefined;
                }
            },

            //加载三维模型
            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 => {
                        xt3d.TilesetPlugin.setTilesetHeight(tileset, 58);
                    })
                    .otherwise(function(error) {
                        console.log(error);
                    });
            },

            //设置视角
            setView() {
                let flyToOpts = {
                    destination: {
                        x: -1574022.5720305464,
                        y: 5327801.903466489,
                        z: 3123111.566829993
                    },
                    orientation: {
                        heading: 4.979524808068254,
                        pitch: -0.54024894684202,
                        roll: 6.279951136096583
                    }
                };
                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 在线预览地址

继续阅读