简体   繁体   English

setFeatureState 不更新地图框中的值?

[英]setFeatureState not updating value in mapbox?

I am trying to update the stroke of a circle when a circle is clicked in mapbox.当在 mapbox 中单击一个圆圈时,我正在尝试更新一个圆圈的笔划。 I've followed the documentation from mapbox but it doesnt seem to be updating the values.我遵循了 mapbox 的文档,但它似乎没有更新值。 Console is all clear too.控制台也很清楚。

  t.map.addLayer({
    id: id,
    type: 'circle',
    source: id,
    paint: {
      'circle-radius': 100,
      'circle-opacity': 0.5,
      'circle-color': 'rgba(20, 106, 181, 0.11)',
      'circle-stroke-color': [
        'case', ['boolean', ['feature-state', 'clicked'], false],
          'rgba(20, 106, 181, 0.11)',
          '#146ab5',
      ],
      'circle-stroke-width': [
        'case', ['boolean', ['feature-state', 'clicked'], false],
          2,
          0,
      ],
    },
  });

Im then trying to update the state:然后我尝试更新 state:

  t.map.on('click', id, (e) => {
    t.map.setFeatureState({ source: id, id: id }, { clicked: true });
  });

I've slowely stepped through but the values do not update.我已经慢慢完成了,但值没有更新。 Thanks谢谢

Reviewing your example it appears that your id reference is not correct.查看您的示例,您的 id 参考似乎不正确。 For each element in the layer, a unique ID must exist, this can easily be added to the source data.对于层中的每个元素,必须存在唯一的 ID,这可以很容易地添加到源数据中。

t.map.addSource('sourceId', {
    'type': 'dataType',
    'data': yourData,
    'generateId': true // This ensures that all features have unique IDs
});

Furthermore once this has been added you must then locate and reference this unique id in each setFeatureState update:此外,一旦添加了它,您必须在每个 setFeatureState 更新中找到并引用这个唯一的 id:

t.map.on('click', id, (e) => {
    let uniqueId = e.features[0]['id'];
    t.map.setFeatureState({ source: id, id: uniqueId }, { clicked: true });
  });

I myself am struggling away at a similar issue right now, hope that this input has helped (I am by no means an expert with MapBox GL JS).我自己现在正在努力解决类似的问题,希望这些意见有所帮助(我绝不是 MapBox GL JS 的专家)。 The above examples are adopted from Interactive Hover Effects , and Create Hover Effect in the MapBox documentation.以上示例取自Interactive Hover Effects和 MapBox 文档中的Create Hover Effect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM