Cesium加载GeoJson数据并将平面拉伸为立体

注意:我在使用Cesium会出现跨域问题,所以我使用nginx做的代理

1、准备工作

先去cesium ion申请token

2、cesium加载geojson数据

geojson数据可以通过此地址自己绘制生成geojson数据https://geojson.io

<div id="cesiumContainer"></div>
var cesiumAsset = '你的token';
        Cesium.Ion.defaultAccessToken = cesiumAsset;
        const viewer = new Cesium.Viewer('cesiumContainer', {
            homeButton: false,              //home按钮
            sceneModePicker: false,         //模式切换按钮
            baseLayerPicker: false,         //右上角的图层选择按钮
            navigationHelpButton: false,    //右上角的帮助按钮
            animation: false,               //左下角的动画仪表盘
            timeline: false,                //底部的时间轴
            fullscreenButton: false,        //右下角的全屏按钮
            vrButton: false,                //vr按钮
            infoBox: false,                 //点击实体的信息框
            selectionIndicator: false,      //点击实体的绿色小框
            geocoder: false,                //搜索框
        });
        viewer._cesiumWidget._creditContainer.style.display = "none";		//隐藏cesium ion

这样,你的地球就加载出来了
然后添加geojson数据到地球上

 Cesium.GeoJsonDataSource.load('http://localhost:3000/json/test1.json', {
            stroke: Cesium.Color.fromCssColorString('rgba(0, 0, 0, 1)'),
            fill: Cesium.Color.PINK.withAlpha(0.5),
            strokeWidth: 3
        });

3 将加载的geojson数据立体拉伸

我这里是通过设置extrudedHeight设置实体的高度,在通过height设置离地面的高度来实现一层一层的效果,当然也可以只设置extrudedHeight实现拉伸效果

 Cesium.GeoJsonDataSource.load('http://localhost:3000/json/test1.json', {
            stroke: Cesium.Color.fromCssColorString('rgba(0, 0, 0, 1)'),
            //fill: Cesium.Color.PINK.withAlpha(0.5),
            strokeWidth: 3
        }).then(function (dataSource) {
            viewer.dataSources.add(dataSource);
            var entities = dataSource.entities.values;
            var zrzHeight;
            var zrzLc;
            for (let i = 0; i < entities.length; i++) {
                let entity = entities[i];
                let name = entity.name;
                entity.polygon.outline = true;
                if (entity._properties._isDS._value == 1) {
                    for (var j = 0; j < entity._properties._lc._value; j++) {
                        var entityes = entity;
                        entityes._id = entity._properties._name._value + (j + 1);
                        var en = new Cesium.Entity(entityes);
                        en.polygon.material = Cesium.Color.fromCssColorString('rgba(255,255,255,1)');
                        en.polygon.extrudedHeight = (j + 1) * 5;
                        en.polygon.height = (j * 5);
                        viewer.entities.add(en);
                    }
                }
                else if (entity._properties._isDS._value == -1) {
                    for (var j = 0; j < entity._properties._lc._value; j++) {
                        var entityes = entity;
                        entityes._id = entity._properties._name._value + (j + 1);
                        var en = new Cesium.Entity(entityes);
                        en.polygon.material = Cesium.Color.fromCssColorString('rgba(255,255,255,1)');
                        en.polygon.extrudedHeight = - (j + 1) * 5;
                        en.polygon.height = - (j * 5);
                        viewer.entities.add(en);
                    }
                }
            }
        });

4、设置地球能看到地下

        viewer.scene.screenSpaceCameraController.enableCollisionDetection = false;     //设置鼠标进入地下
        //设置地球透明度
        viewer.scene.globe.translucency.enabled = true;
        viewer.scene.globe.translucency.frontFaceAlphaByDistance = new Cesium.NearFarScalar(400.0, 0.5, 2000, 0.9);

5、定位到指定位置

viewer.camera.flyTo({
            destination: Cesium.Cartesian3.fromDegrees(30.015676845995472, 8.13054316835077, 1000),
            orientation: {
                heading: Cesium.Math.toRadians(348.4202942851978),
                pitch: Cesium.Math.toRadians(-89.74026687972041),
                roll: Cesium.Math.toRadians(0)
            },
            complete: function callback() {
                // 定位完成之后的回调函数
            }
        });

6、完整代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cesium加载GeoJson数据</title>
    <script src="http://localhost:3000/Cesium/Cesium.js"></script>
    <link rel="stylesheet" href="http://localhost:3000/Widgets/widgets.css" />
    <script src="http://localhost:3000/js/jquery.js"></script>
    <style type="text/css">
        html,
        body,
        #cesiumContainer{
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div id="cesiumContainer"></div>
    <script>
        var cesiumAsset = '你的token';
        Cesium.Ion.defaultAccessToken = cesiumAsset;

        const viewer = new Cesium.Viewer('cesiumContainer', {
            homeButton: false,              //home按钮
            sceneModePicker: false,         //模式切换按钮
            baseLayerPicker: false,         //右上角的图层选择按钮
            navigationHelpButton: false,    //右上角的帮助按钮
            animation: false,               //左下角的动画仪表盘
            timeline: false,                //底部的时间轴
            fullscreenButton: false,        //右下角的全屏按钮
            vrButton: false,                //vr按钮
            infoBox: false,                 //点击实体的信息框
            selectionIndicator: false,      //点击实体的绿色小框
            geocoder: false,                //搜索框
        });
        viewer._cesiumWidget._creditContainer.style.display = "none";		//隐藏cesium ion
        viewer.scene.screenSpaceCameraController.enableCollisionDetection = false;     //设置鼠标进入地下
        //设置地球透明度
        viewer.scene.globe.translucency.enabled = true;
        viewer.scene.globe.translucency.frontFaceAlphaByDistance = new Cesium.NearFarScalar(400.0, 0.5, 2000, 0.9);

        // 将三维球定位到指定位置
        viewer.camera.flyTo({
            destination: Cesium.Cartesian3.fromDegrees(30.015676845995472, 8.13054316835077, 1000),
            orientation: {
                heading: Cesium.Math.toRadians(348.4202942851978),
                pitch: Cesium.Math.toRadians(-89.74026687972041),
                roll: Cesium.Math.toRadians(0)
            },
            complete: function callback() {
                // 定位完成之后的回调函数
            }
        });
        
        //加载GeoJson数据
        Cesium.GeoJsonDataSource.load('http://localhost:3000/json/test1.json', {
            stroke: Cesium.Color.fromCssColorString('rgba(0, 0, 0, 1)'),
            //fill: Cesium.Color.PINK.withAlpha(0.5),
            strokeWidth: 3
        }).then(function (dataSource) {
            viewer.dataSources.add(dataSource);
            var entities = dataSource.entities.values;
            var zrzHeight;
            var zrzLc;
            for (let i = 0; i < entities.length; i++) {
                let entity = entities[i];
                let name = entity.name;
                entity.polygon.outline = true;
                if (entity._properties._isDS._value == 1) {
                    for (var j = 0; j < entity._properties._lc._value; j++) {
                        var entityes = entity;
                        entityes._id = entity._properties._name._value + (j + 1);
                        var en = new Cesium.Entity(entityes);
                        en.polygon.material = Cesium.Color.fromCssColorString('rgba(255,255,255,1)');
                        en.polygon.extrudedHeight = (j + 1) * 5;
                        en.polygon.height = (j * 5);
                        viewer.entities.add(en);
                    }
                }
                else if (entity._properties._isDS._value == -1) {
                    for (var j = 0; j < entity._properties._lc._value; j++) {
                        var entityes = entity;
                        entityes._id = entity._properties._name._value + (j + 1);
                        var en = new Cesium.Entity(entityes);
                        en.polygon.material = Cesium.Color.fromCssColorString('rgba(255,255,255,1)');
                        en.polygon.extrudedHeight = - (j + 1) * 5;
                        en.polygon.height = - (j * 5);
                        viewer.entities.add(en);
                    }
                }
            }
        });
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值