gpt4 book ai didi

reactjs - 为什么我在 Mapbox GL 中设置了我的数据集要素 ID 却未定义它们?

转载 作者:行者123 更新时间:2023-12-05 05:42:26 29 4
gpt4 key购买 nike

我很难使用 mapbox GL 设置要素 ID。

我已经 read您可以在源代码中使用 generateId:true 自动生成 ID:

Whether to generate ids for the geojson features. When enabled, thefeature.id property will be auto assigned based on its index in thefeatures array, over-writing any previous values.

除了我想在其他地方使用我的数据,而不仅仅是 mapbox map (除了标记列表);所以我想手动设置它们,因为我希望能够从我的列表中定位我在 map 上的特征。所以,我不想在这里使用 generateId:true

In the doc ,他们的数据集示例就像

  {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id": "marker-iv1qi3x10",//an ID here
"title": "Burnham Park",
"description": "A lakefront park on Chicago's south side.",
"marker-size": "medium",
"marker-color": "#1087bf",
"marker-symbol": "marker-blue"
},
"geometry": {
"coordinates": [
-87.603735,
41.829985
],
"type": "Point"
},
"id": "0de616c939ce2f31676ff0294c78321b"//another ID here
}
]
}

所以他们在特征对象中有一个 ID "id": "0de616c939ce2f31676ff0294c78321b",在该特征的属性中有另一个 ID "id": "marker-iv1qi3x10".

我猜 mapbox 在内部用于功能的 ID(当 generateId 在源代码中设置为 true 时自动生成)是第一个。

假设我手动设置 ID:

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id": "customPropId01"
},
"geometry": {
"coordinates": [
-87.603735,
41.829985
],
"type": "Point"
},
"id": "customID01"
}
]
}

在加载源后检查数据时,我的自定义 ID 仍然存在(使用此代码)。

//when a specific source has been loaded
map.on('sourcedata', (e) => {
if (e.sourceId !== 'markers') return;
if (!e.isSourceLoaded) return;
console.log("SOURCE DATA LOADED",e.source);
});

但是当我点击 map 上的标记并记录它时,我的要素的 ID 属性已被删除,现在是未定义:

enter image description here

我没有使用我的输入源数据来列出我的标记,而是查看了 querySourceFeatures ,但这并没有帮助,因为它只返回 map 边界框中的特征 - 我希望我的列表显示所有特征,这就是为什么我需要在那里使用“原始”源数据。

这让我发疯。有谁知道为什么未设置 ID 以及我该如何解决这个问题?

谢谢!

最佳答案

您的 ID 是一个字符串。但是如果你想保留你的 id,这个字符串应该被转换成整数 like the doc explain :

Features are identified by their id attribute, which must be an integer or a string that can be cast to an integer.

关于reactjs - 为什么我在 Mapbox GL 中设置了我的数据集要素 ID 却未定义它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72040370/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com