Cesium 加载面数据 点数据 label

发布时间 2023-10-24 16:02:33作者: 小七要走

Cesium 加载geojosn 数据

export const cesiumUtils = {
    viewer: null,
    dataSourceArr: [],
    addDataSource(geojsonData, config) {
  
      Cesium.GeoJsonDataSource.load(geojsonData, {
        stroke: Cesium.Color.HOTPINK,
        fill: Cesium.Color.PINK,
        strokeWidth: 3
      }).then((dataSource) => {
        const tmpDataSource = new Cesium.CustomDataSource(Date.now().toString());
        const entities = dataSource.entities.values;
        for (let index = 0; index < entities.length; index++) {
          const element = entities[index];
          let polyPositions = element.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
          let polyCenter = Cesium.BoundingSphere.fromPoints(polyPositions).center;
          const pos = element.polygon.hierarchy.getValue().positions;
          let newEntity = new Cesium.Entity({
            position:polyCenter,
            polygon: {
              hierarchy: element.polygon.hierarchy.getValue(),
              classificationType: Cesium.ClassificationType.BOTH,
              material: Cesium.Color.fromCssColorString(config.color).withAlpha(0.1),
            },
            polyline: {
              positions: [...pos, pos[0]],
              clampToGround: true,
              material: Cesium.Color.fromCssColorString(config.color).withAlpha(1),
              width: 3
            },
            label:{
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                text: config.name,
                font: "14px 宋体",
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                  0.0,
                  5000000.0
                ),
                fillColor: Cesium.Color.WHITE,
                outlineColor: Cesium.Color.WHITE,
                showBorder:false,
                style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                verticalOrigin: Cesium.VerticalOrigin.TOP,
                pixelOffset: new Cesium.Cartesian2(0, -50),
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            }
          })
          tmpDataSource.entities.add(newEntity)
        }
        this.viewer.dataSources.add(tmpDataSource);
        this.dataSourceArr.push(tmpDataSource);
      });
    },
    clearDataSource() {
      for (let i = 0; i < this.dataSourceArr.length; i++) {
        const element = this.dataSourceArr[i];
        this.viewer.dataSources.remove(element, true);
      }
      this.dataSourceArr = [];
    },
  };

注意问题  

如果geojson 数据有z值设置贴地会不起作用