开源

Cesium简单遮罩

2023-04-26  本文已影响0人  YGIS
效果

参考资料

  1. cesium只展示某个区域市省地图
  2. 百度api-掩模-只展示地图指定区域

特点

实现过程

按示例实现

出现黑线问题

优化

processGeoJSONData(data) {
      const list = [];
      data.features.forEach((feature) => {
        const { coordinates, type } = feature.geometry;
        if (type === "MultiPolygon") {
          coordinates.forEach((part) => {
            const coords = [];
            part[0].forEach((point) => {
              coords.push(...point);
            });
            list.push(coords);
          });
        } else {
          const coords = [];
          coordinates[0].forEach((point) => {
            coords.push(...point);
          });
          list.push(coords);
        }
      });
      return list;
    }
getWallPrimitive(list, height = 1000) {
      const wallInstances = list.map((coords) => {
        const positions = Cesium.Cartesian3.fromDegreesArray(coords);
        const wall = new Cesium.WallGeometry({
          positions: positions,
          maximumHeights: positions.map(() => {
            return height;
          }),
        });
        return new Cesium.GeometryInstance({
          geometry: wall,
        });
      });
      const wallPrimitive = new Cesium.Primitive({
        geometryInstances: wallInstances,
        appearance: new Cesium.MaterialAppearance({
          material: Cesium.Material.fromType("Image", {
            image: "./assets/wall.png",
          }),
        }),
      });
      return wallPrimitive;
    },
loadMaskPrimitive(layer) {
      fetch("./data/taizhou.geojson")
        .then((response) => {
          if (response.ok) {
            return response.json();
          } else {
            throw new Error("请求失败!");
          }
        })
        .then((data) => {
          console.log(data);
          const list = this.processGeoJSONData(data);
          const polygonPrimitive = this.getPolygonPrimitive(list);
          const wallPrimitive = this.getWallPrimitive(list);
          layer._delegate.add(polygonPrimitive);
          layer._delegate.add(wallPrimitive);
        })
        .catch((error) => {
          console.log(error);
        });
    },
上一篇 下一篇

猜你喜欢

热点阅读