在mapbox中如何将要素移到顶部(z-index明智)?

问题描述 投票:0回答:1

我有一个充满状态边界“功能”的图层。当用户点击状态时,我想将该状态的特征移动到堆栈顶部(z-index wise)。

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);

这似乎对数组做了一些非常疯狂的事情(复制一些状态,删除其他状态)。不知道发生了什么

mapbox mapbox-gl-js
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.