Mapbox图层功能重复ID

4

我正在使用Mapbox-GL-JS构建一个Web应用程序,它使用Mapbox Studio平铺集将图层作为流域地图添加。然而,当我访问图层时,多个要素似乎接收到相同的ID。

这里提到了这是由于我的数据引起的,但是数据集中没有ID字段: https://www.dropbox.com/s/5ci0hosqakvdahx/townships.json?dl=0

在这里有一个示例演示了此问题。

map.on("mousemove", "gemeentes", function (e) {
    if (e.features.length > 0) {
        if (hoverGemeenteId) {
            map.setFeatureState({
                source: 'gemeentes-src',
                sourceLayer: 'townships-0u4ffk',
                id: hoverGemeenteId
            }, {hover: false});
        }
        hoverGemeenteId = e.features[0].id;
        console.log(hoverGemeenteId);
        map.setFeatureState({
            source: 'gemeentes-src',
            sourceLayer: 'townships-0u4ffk',
            id: hoverGemeenteId
        }, {hover: true});
    }
});

以下是项目示例内容:

https://docs.mapbox.com/mapbox-gl-js/example/hover-styles/

当鼠标悬停在一个城镇上时,它会改变颜色,但由于有多个具有相同ID的城镇,因此多个区域会发光,而不仅仅是悬停的那一个。

编辑: 当我运行以下代码时:

const filtered = map.querySourceFeatures('gemeentes-src', {
        sourceLayer: 'townships-0u4ffk',
        filter: ['>=', ['id'], 0]
    });

很清楚地显示有多个具有相同ID的特征,且没有一个ID超过249。就好像存在一个250的上限,新特征就从0开始。


我不知道为什么,但是那个 JSBin 对我来说只呈现一个白色页面。我可以看到它正在获取 Mapbox 资源,但是没有任何东西显示出来。 - Steve Bennett
1个回答

5

添加数据源时,您可以使用 promoteId 选项基于属性为要素分配唯一的ID。

https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/#vector-promoteId

例如(来自向MapboxGL添加此功能的 PR):

map.addSource('counties', {
    "type": "vector",
    "url": "mapbox://mapbox.82pkq93d",
    "promoteId": {"original": "COUNTY"}
});
...
map.setFeatureState({
    source: 'counties', 
    sourceLayer: 'original', 
    id: 'Marin County' // reference a feature by a county name for feature-state
}, {hover: true});

4
谢谢!应该说明“promoteId”: {"original": "COUNTY"} 中的“original”是一个“source-layer”。 - SERG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接