给cesium添加信号线

5 篇文章 0 订阅
3 篇文章 1 订阅

使用的是websocket通信接收数据,每条数据来了之后需要更新点到点之间的连线
核心代码:
首次加载

 		    viewer.entities.add({
              name: "Purple straight arrow at height",
              id: ele.id + "" + ele.senderToConnect,
              polyline: {
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(
                  ele.coordinates
                ),
                width: 3,
                arcType: Cesium.ArcType.NONE,
                material: new Cesium.PolylineArrowMaterialProperty(
                  Cesium.Color.PURPLE
                ),
              },
            });```

其中id为两个点的数据组成的id,作为唯一标识,方便后面再进行查找,如果不添加的话,cesium会添加一个随机id
例如:
在这里插入图片描述
添加后就是自己所添加的id:
在这里插入图片描述

可以根据viewer.entities.getById()方法拿到对应的实体;
后面新的数据到来之后进行更新:

			//	hasLine判断是否存在该实体,根据第一个添加进去的两个id
   			 hasLine = viewer.entities.getById(ele.id + "" + ele.senderToConnect);
 			if (hasLine) {
            hasLine.polyline.positions =
              Cesium.Cartesian3.fromDegreesArrayHeights(ele.coordinates);
          } 

整体代码:

  const entityInfo = JSON.parse(msg.data); //接收到的sockit信息
      // 添加信号线
      let hasLine = {};
      if (entityInfo.modelSignal) {
        entityInfo.modelSignal.forEach((ele) => {
          // console.log("信号线处理", Date.parse(new Date()));
          hasLine = viewer.entities.getById(ele.id + "" + ele.senderToConnect);
          if (hasLine) {
            hasLine.polyline.positions =
              Cesium.Cartesian3.fromDegreesArrayHeights(ele.coordinates);
          } else {
            viewer.entities.add({
              name: "Purple straight arrow at height",
              id: ele.id + "" + ele.senderToConnect,
              polyline: {
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(
                  ele.coordinates
                ),
                width: 3,
                arcType: Cesium.ArcType.NONE,
                material: new Cesium.PolylineArrowMaterialProperty(
                  Cesium.Color.PURPLE
                ),
              },
            });
          }
        });
      }

其中数据格式为:
在这里插入图片描述
modelSignal数组中存放的是需要添加的信号线的信息,由两个点组成,
其中coordinates中的前三个元素为第一个点,后三个是第二个点;(这是cesium画线的方法所规定的)

效果:
在这里插入图片描述
随着数据不断更新,也会不断更新

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值