4.GeoJSON图层的add和remove

2023-08-01  本文已影响0人  xueyueshuai

矢量图层(加载删除)

   // 加载矢量图层
    fetch('http://localhost:8899/geoserver/one/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=one%3Acountry&maxFeatures=100000&outputFormat=application%2Fjson').then(res => res.json()).then(jsonData => {
      delete jsonData.crs
      Cesium.GeoJsonDataSource.load(
        jsonData,
        //options
        {
          stroke: Cesium.Color.BLUE,
          fill: Cesium.Color.PINK.withAlpha(0.8),
          strokeWidth: 3,
        },
      ).then(dataSource => {
        dataSource.name = 'provinceLayer'
        this.viewer.dataSources.add(dataSource);
      })
    })
    
    // 删除矢量图层
    setTimeout(() => {
      console.log('start-del')
      // 删除方式1
      // for (let i = 0; i < this.viewer.dataSources.length; i++) {
      //   let dataSource = this.viewer.dataSources.get(i)
      //   if(dataSource.name === 'provinceLayer'){
      //     this.viewer.dataSources.remove(dataSource) // this.viewer.dataSources.remove(dataSource) 或者是 dataSource.show=false
      //   }
      // }
      
      // 删除方式2
      let dataSource = this.viewer.dataSources.getByName('provinceLayer')[0]
      this.viewer.dataSources.remove(dataSource) // this.viewer.dataSources.remove(dataSource) 或者是 dataSource.show=false
    }, 5000)
removeAll()
上一篇 下一篇

猜你喜欢

热点阅读