代码之家  ›  专栏  ›  技术社区  ›  Toli

在mapbox中,如何将要素移到顶部(按z索引)?

  •  0
  • Toli  · 技术社区  · 7 年前

    export function drawStateBorders() {
      $.getJSON('https://www.mapbox.com/mapbox-gl-js/assets/us_states.geojson').then((data) => {
        this.stateGeoJSON = data;
    
        this.map
          .addSource('states', {
            type: 'geojson',
            data,
          })
          .addLayer({
            id: 'state-borders',
            type: 'line',
            source: 'states',
            paint: {
              'line-color': [
                'case', ['boolean', ['feature-state', 'selected'], false],
                '#8d8b76',
                '#bfe2ab',
              ],
              'line-width': [
                'case', ['boolean', ['feature-state', 'selected'], false],
                6,
                3,
              ],
            },
          });
      });
    }
    

    当我选择状态时

    export function stateSelected(state) {
      const stateFeatures = this.map.queryRenderedFeatures({
        layers: ['state-borders'],
        filter: [
          '==', 'STATE_NAME', state,
        ],
      });
    
      const features = this.stateGeoJSON.features;
      const currentFeature = stateFeatures[0];
    
      if (!currentFeature) {
        return;
      }
    
      // same state
      if (currentFeature.id === this.selectedStateId) return;
    
      // move to front HERE ?????
    
      // old selected state
      if (this.selectedStateId) {
        this.map.setFeatureState({
          source: 'states',
          id: this.selectedStateId,
        }, {
          selected: false,
        });
      }
    
      this.selectedStateId = currentFeature.id;
    
      this.map.setFeatureState({
        source: 'states',
        id: this.selectedStateId,
      }, {
        selected: true,
      });
    }
    

    到目前为止我试过

      features.splice(features.indexOf(currentFeature), 1);
      features.push(currentFeature);
      this.map.getSource('states').setData(this.stateGeoJSON);
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Toli    7 年前

    将状态添加到另一层有效(感谢@anderwhavey的建议)。

    如果有人感兴趣这里是我的代码

    export function stateSelected(state) {
      const features = this.stateGeoJSON.features;
      const currentFeature = features.find(s => s.properties.NAME === state);
    
      if (!currentFeature) {
        return;
      }
    
      // removes active layer
      this.removeLayersContaining('state-borders-active');
      this.drawActiveStateLayer(currentFeature);
    }
    
    
    export function drawActiveStateLayer(feature) {
      this.map
        .addLayer({
          id: 'state-borders-active',
          type: 'line',
          source: {
            type: 'geojson',
            data: feature
          },
          paint: {
            'line-color': '#8d8b76',
            'line-width': 6,
          },
        });
    }
    
    export function drawStateBorders() {
      $.getJSON('states.json').then((data) => {
    
        this.stateGeoJSON = data;
    
        this.map
          .addSource('states', {
            type: 'geojson',
            data,
          })
          .addLayer({
            id: 'state-borders',
            type: 'line',
            source: 'states',
            paint: {
              'line-color': '#bfe2ab',
              'line-width':  3,
            },
          });
      });
    

    这也是我正在使用的shapefile: http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_500k.json

    推荐文章