layers + features

2023-06-27  本文已影响0人  xueyueshuai

获取所有图层


      this.map.getLayers().forEach(layer=>{
        console.log(layer)
      })

获取所有矢量图层的所有要素

          layer.getSource().getFeatures().forEach(feature=>{
            console.log(feature)
            console.log(feature.getId())
            console.log(feature.get('xxx'))
          })

要素的获取 添加 删除

          let feature = layer.getSource().getFeatureById('base-old.2')
          console.log(feature)

          layer.getSource().removeFeature(feature);
          
          setTimeout(()=>{
            layer.getSource().addFeatures([feature]);
          },2000)
      let getLayerByK = (map, k = 'name', v = 'oldBaseLayer') => {
        let findLayer = null
        this.map.getLayers().forEach(layer => {
          if (layer.get(k) === v) {
            findLayer = layer
          }
        })
        return findLayer
      }
      
      let oldBaseLayer = getLayerByK(this.map, 'name', 'oldBaseLayer')
      console.log(oldBaseLayer)
      if(oldBaseLayer){
        let feature = oldBaseLayer.getSource().getFeatureById('base-old.2')
        console.log(feature)
        if(feature) {
          oldBaseLayer.getSource().removeFeature(feature);
          setTimeout(()=>{
            oldBaseLayer.getSource().addFeatures([feature]);
          },2000)
        }
      }
上一篇 下一篇

猜你喜欢

热点阅读