【Cesium】学习记录0x02(添加实体)

351 阅读2分钟

能井

image.png

配置viewer

        //配置viewer
        const viewer=new Cesium.Viewer("cesiumContainer",{
            animation:false,
            sceneMode:Cesium.SceneMode.SCENE3D,
            sceneModePicker:false,
            baseLayerPicker:false,
            geocoder:false,
            timeline:false,
            fullscreenButton:false,
            homeButton:false,
            vrButton:false,
            terrainProvider : Cesium.createWorldTerrain(),
            navigationHelpButton:false,
            
            imageryProvider : new Cesium.OpenStreetMapImageryProvider({
                    url : 'https://a.tile.openstreetmap.org/'
                }),
            
            
        })
        //关闭官方平台自带的一些功能按键
        //其中版权信息方面无法修改,只能保持打开

通过entities添加实体

//添加实体
        //添加信息内容
        var redBox = viewer.entities.add({
            name : 'Red box with black outline',
            position: Cesium.Cartesian3.fromDegrees(-107.0, 40.0, 300000.0),
            box : {
                dimensions : new Cesium.Cartesian3(400000.0, 300000.0, 500000.0),
                material : Cesium.Color.DEEPPINK.withAlpha(0.5),
                outline : true,
                outlineColor : Cesium.Color.GREEN
            }
            });

        //异步设置摄像机以查看提供的一个或多个实体或数据源。如果数据源仍在加载过程中,或者可视化仍在加载中,此方法在执行缩放之前等待数据准备就绪。
        //偏移量是在以边界球中心为中心的局部东北向上参考框中的航向/俯仰/范围。航向角和俯仰角是在局部的东西向北参考系中定义的。航向是从y轴到x轴的角度。
        //间距是从xy平面开始的旋转。正螺距角度在平面上方。负俯仰角在平面下方。范围是到中心的距离。如果范围是零,则将计算范围以使整个边界球都可见。
        // 在2D模式下,必须有一个俯视图。摄像机将被放置在目标上方并向下看。上方的高度目标将是范围。航向将根据偏移量确定。如果标题不能根据偏移量确定,航向将为北。
        viewer.zoomTo(viewer.entities);//提供查看模型实体

需要注意这里的viewer.entities.add(option)这个函数,该函数是entitiescollection类型自带的方法,接收一个entity option,即一个模型对象的所有配置,该配置列表如下图所示 image.png

通过CZML添加

CZML包括了点、线、地标、模型和其它的一些图形元素,并指明了这些元素如何随时间而变化。

        var czml = [{
            "id" : "document",
            "name" : "box",
            "version" : "1.0"
        },{
            "id" : "shape2",
            "name" : "Red box with black outline",
            "position" : {
                "cartographicDegrees" : [-107.0, 40.0, 300000.0]
            },
            "box" : {
                "dimensions" : {
                    "cartesian": [400000.0, 300000.0, 500000.0]
                },
                "material" : {
                    "solidColor" : {
                        "color" : {
                            "rgba" : [251, 74, 71, 128]
                        }
                    }
                },
                "outline" : true,
                "outlineColor" : {
                    "rgba" : [0, 255, 0, 1]
                }
            }
        }];

        var dataSourcePromise = Cesium.CzmlDataSource.load(czml);
        viewer.dataSources.add(dataSourcePromise);
        viewer.zoomTo(dataSourcePromise);