Cesium|xt3d加载天地图底图

这篇博客展示了如何使用Cesium JavaScript库与XT3D插件来加载并切换天地图的卫星地图、电子地图和地形图。通过提供的代码示例,读者可以了解到如何初始化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">

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

    <!--  引入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%);
        }
        
        button {
            background: transparent;
            border: 1px solid #00d0ffb8;
            color: white;
            padding: 7px 9px;
            border-radius: 2px;
            margin: 3px;
            cursor: pointer
        }
    </style>
</head>

<body>
    <div id="map3d"></div>
    <div class="btn-container">
        <button onclick="changeMap(0)">卫星地图</button>
        <button onclick="changeMap(1)">电子地图</button>
        <button onclick="changeMap(2)">地形图</button>
    </div>

    <script>
        let xt3dInit = {
            init(el) {
                this.initViewer(el);
            },

            //初始化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://t5.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=tdtTk"
                    })
                });

                //默认添加注记图层
                this.viewer.imageryLayers.addImageryProvider(new Cesium.UrlTemplateImageryProvider({
                    url: "https://t5.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=tdtTk"
                }));
            },

            //改变底图
            changeMap(mapIndex) {
                this.viewer.imageryLayers.removeAll(true); //删除所有底图
                let imageryProvider; //天地图访问需要加Token 请自己申请token 否则可能访问失败
                switch (mapIndex) {
                    case 0: //影像
                        imageryProvider = new Cesium.UrlTemplateImageryProvider({
                            url: "https://t5.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=tdtTk"
                        })
                        break;
                    case 1: //电子
                        imageryProvider = new Cesium.UrlTemplateImageryProvider({
                            url: "https://t5.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=tdtTk"
                        })
                        break;
                    case 2: //地形
                        imageryProvider = new Cesium.UrlTemplateImageryProvider({
                            url: "https://t5.tianditu.gov.cn/DataServer?T=ter_w&x={x}&y={y}&l={z}&tk=tdtTk"
                        })
                        break;
                }
                this.viewer.imageryLayers.addImageryProvider(imageryProvider);
                //默认添加注记图层
                this.viewer.imageryLayers.addImageryProvider(new Cesium.UrlTemplateImageryProvider({
                    url: "https://t5.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=tdtTk"
                }));
            },

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

        xt3dInit.init("map3d");

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

</html>

预览地址

xt3d 在线预览地址

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xt3d

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

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

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

打赏作者

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

抵扣说明:

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

余额充值