javascript - setFeatureState 不更新 Mapbox 中的值

标签 javascript mapbox

我正在尝试更改标记的颜色,该标记是一个圆圈,正在使用图层上的 paint 属性进行绘制。

按照本教程:
https://docs.mapbox.com/mapbox-gl-js/example/hover-styles/

我设置了circle-color依赖于 feature-state :

          map.addLayer({
        id: "singles",
        type: "circle",
        source: "users",
        filter: ["!has", "point_count"],
        paint: {
            'circle-radius': {
                'base': 10,
                'stops': [[5, 20], [15, 500]]
            },
            'circle-color':  ["case",
            ["boolean", ["feature-state", "hover"], false],
            '#ddffc8',
            '#ff0000'
            ],
          }
      });

然后,当有人将鼠标悬停在侧边栏上时,我想更新功能状态并更改颜色:
function highlightMarkersOnHoverOnSidebar (markers, map) {
  let marks = markers
  Array.from(document.querySelectorAll('.sideBarItems')).map( (x, i) => {

    x.addEventListener('mouseenter', function(){

      map.setFeatureState({source: 'users', id: marks.features[i].properties.id}, { hover: true});
    }, false)

    x.addEventListener('mouseleave', function(){
      map.setFeatureState({source: 'users', id: marks.features[i].properties.id}, { hover: false});

    }, false)
  })
}

但是,当我将侧边栏元素悬停时,什么也没有发生。它甚至不会引发错误。

我有什么遗漏吗?谢谢。

最佳答案

我也遇到了这个问题。
似乎您需要在 geojson 中的特征级别有一个 id。不在属性中:

{
  "type": "FeatureCollection",
  "features": [
    {
      "id": 4459,
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          8.64543,
          50.163105
        ]
      },
      "properties": {
        "id": "NOT HERE",
        "name": "Test",
        "foo": "bar",
      }
    }
  ]
}
将 id 移至功能解决了该问题。

关于javascript - setFeatureState 不更新 Mapbox 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56856964/

相关文章:

javascript - 在 JavaScript 中用字符串调用类

javascript - 从 HTML 创建 JavaScript 数组

javascript - 在 MongoDB 聚合中返回带数据的计数

ios - 条件绑定(bind)的初始化程序必须具有可选类型,而不是 '(CLLocationCoordinate2D, String, UIColor) -> UIView' ?

mapbox - 将自定义标记添加到 mapbox gl

javascript - 在 Mapbox 单击标记上运行函数//在相对于 Mapbox 坐标的位置显示 SVG

javascript - Three.js自定义几何体旋转问题

javascript - 动态添加长度为 0 的 div?

javascript - MapBox - 从 Javascript 搜索标记

javascript - 错误的 Leaflet.js 平铺位置,在拖动时卡入到位