详细cesium Entity添加实体

16 篇文章 0 订阅

先看官网的apiEntity - Cesium Documentation

这些都是属性。

看官网的代码 创建实体 – Cesium

const viewer = new Cesium.Viewer("cesiumContainer");

const wyoming = viewer.entities.add({
  polygon: {
    hierarchy: Cesium.Cartesian3.fromDegreesArray([
      -109.080842, 45.002073, -105.91517, 45.002073, -104.058488, 44.996596,
      -104.053011, 43.002989, -104.053011, 41.003906, -105.728954, 40.998429,
      -107.919731, 41.003906, -109.04798, 40.998429, -111.047063, 40.998429,
      -111.047063, 42.000709, -111.047063, 44.476286, -111.05254, 45.002073,
    ]),
    height: 0,
    material: Cesium.Color.RED.withAlpha(0.5),
    outline: true,
    outlineColor: Cesium.Color.BLACK,
  },
});

viewer.zoomTo(wyoming);

const redEllipse = viewer.entities.add({
  position: Cesium.Cartesian3.fromDegrees(-103.0, 40.0),
  name: "Red ellipse on surface",
  ellipse: {
    semiMinorAxis: 250000.0,
    semiMajorAxis: 400000.0,
    material: Cesium.Color.RED.withAlpha(0.5),
  },
});

const blueEllipse = viewer.entities.add({
  position: Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 100000.0),
  name: "Blue translucent, rotated, and extruded ellipse with outline",
  ellipse: {
    semiMinorAxis: 150000.0,
    semiMajorAxis: 300000.0,
    extrudedHeight: 200000.0,
    rotation: Cesium.Math.toRadians(45),
    material: Cesium.Color.BLUE.withAlpha(0.5),
    outline: true,
  },
});

也可以

// 初始化 Cesium Viewer
const viewer = new Cesium.Viewer('cesiumContainer');

// 添加一个点实体
const pointEntity = viewer.entities.add({
    name: 'My Point',
    position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883), // 经度和纬度
    point: {
        pixelSize: 10,
        color: Cesium.Color.RED,
        outlineColor: Cesium.Color.BLACK,
        outlineWidth: 2,
    },
    label: {
        text: 'Hello, Cesium!',
        font: '14px Helvetica',
        fillColor: Cesium.Color.WHITE,
        outlineColor: Cesium.Color.BLACK,
        outlineWidth: 2,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
    },
});

// 如果需要聚焦到这个实体
viewer.zoomTo(pointEntity);

之前的练习。

// 实体 entity
    // const point = new Cesium.Entity({
    //     position:Cesium.Cartesian3.fromDegrees(109.4776560,36.6517925),
    //     point:{
    //         pixelSize:20,
    //         color:Cesium.Color.BLUE,
    //     }
    // })
    // // 添加到地图里
    // viewer.entities.add(point)
    // viewer.zoomTo(point)//地图跳转

    // 写法二
    //    const point2= viewer.entities.add({
    //         id:'point',
    //         position:Cesium.Cartesian3.fromDegrees(140,30),
    //         point:{
    //             pixelSize:20,
    //             color:Cesium.Color.BLUE
    //         }
    //     })
    //     console.log("point",point2);
    
    // 图片坐标
    // const billboard=viewer.entities.add({
    //     position:Cesium.Cartesian3.fromDegrees(-75,60,10),
    //     billboard:{
    //         image:'../../public/img/position.png',
    //         scale:0.1,
    //         color:Cesium.Color.BLUE
    //     }
    // })
    // 文字
    // const label =viewer.entities.add({
    //     position:Cesium.Cartesian3.fromDegrees(-75,40,10),
    //     label:{
    //         text:"cesium",
    //         fillColor:Cesium.Color.YELLOWGREEN,//变量
    //         showBackground:true,
    //         backgroundColor:new Cesium.Color(255,0,0),//实例化类

    //     }
    // })
    // 线
    // const polyline=viewer.entities.add({
    //     polyline:{
    //         positions:Cesium.Cartesian3.fromDegreesArray([-75,41,-75,40.5,-75.5,40.5]),
    //         width:10,
    //         material:Cesium.Color.YELLOWGREEN,
    //     }
    // })
    // viewer.zoomTo(polyline)

对于添加实体的封装

addPointEntity(lngLatHeight, option = {}) {
    if (lngLatHeight.length < 3) {
        throw new Error('lngLatHeight must contain [lng, lat, height]');
    }

    let newOption = Object.assign({}, option);
    let positionTemp = Cesium.Cartesian3.fromDegrees(lngLatHeight[0], lngLatHeight[1], lngLatHeight[2]);
    newOption.color = new Cesium.Color.fromCssColorString(option.color || '#FF0000').withAlpha(option.alpha || 1.0);

    if (option.outlineColor) {
        newOption.outlineColor = new Cesium.Color.fromCssColorString(option.outlineColor);
    }

    let pointConfig = {
        position: positionTemp,
        point: {
            ...newOption
        }
    };

    if (newOption.id) {
        pointConfig.id = newOption.id;
    }

    return this.viewer.entities.add(pointConfig);
}

1. 函数定义

addPointEntity(lngLatHeight, option = {}) {

  • 参数
    • lngLatHeight:一个数组,包含经度、纬度和高度。
    • option:一个对象,包含点的样式和属性,默认为空对象。

2. 输入验证

if (lngLatHeight.length < 3) { throw new Error('lngLatHeight must contain [lng, lat, height]'); }

  • 该部分检查 lngLatHeight 数组是否至少包含三个元素,确保传入了有效的经度、纬度和高度。如果不满足条件,则抛出错误。

3. 创建新选项

let newOption = Object.assign({}, option);

  • 使用 Object.assign 创建 option 的浅拷贝,避免直接修改传入的对象。

后面重新写一篇介绍深拷贝 和浅拷贝

4. 计算位置

let positionTemp = Cesium.Cartesian3.fromDegrees(lngLatHeight[0], lngLatHeight[1], lngLatHeight[2]);

  • 使用 Cesium.Cartesian3.fromDegrees 将经纬度和高度转换为 Cesium 可识别的三维笛卡尔坐标。

5. 处理颜色

newOption.color = new Cesium.Color.fromCssColorString(option.color || '#FF0000').withAlpha(option.alpha || 1.0);

  • 将传入的颜色字符串转换为 Cesium 的 Color 对象,并设置透明度。如果未提供颜色和透明度,则使用默认值(红色和不透明)。

6. 处理轮廓颜色

if (option.outlineColor) { newOption.outlineColor = new Cesium.Color.fromCssColorString(option.outlineColor); }

  • 如果提供了轮廓颜色,则将其转换为 Color 对象。

7. 配置点实体

let pointConfig = { position: positionTemp, point: { ...newOption } };

  • 创建一个配置对象 pointConfig,包含位置和点的样式。

8. 添加 ID

if (newOption.id) { pointConfig.id = newOption.id; }

  • 如果提供了 ID,将其添加到配置中。

9. 添加实体

return this.viewer.entities.add(pointConfig);

  • 使用 this.viewer.entities.add 方法将配置添加到 Cesium 的实体集合中,并返回创建的实体。

总结

这段代码的主要作用是将一个点实体添加到 Cesium 视图中,支持多种自定义样式。它确保了输入的有效性,并提供了默认值,增加了函数的鲁棒性和灵活性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值