gor*_*die 5 reactjs mapbox-gl-js
我很难使用 Mapbox GL 设置功能 ID。
我读到generateId:true您可以在源中使用自动生成 ID :
是否为geojson特征生成id。启用后,feature.id 属性将根据其在 features 数组中的索引自动分配,覆盖任何以前的值。
除了我想在其他地方使用我的数据而不仅仅是mapbox地图(旁边的标记列表);所以我想手动设置它们,因为我希望能够从我的列表中定位地图上的功能。所以,我不想generateId:true在这里使用。
在文档中,他们的数据集示例如下
{
"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
}
]
}
Run Code Online (Sandbox Code Playgroud)
因此,它们在要素对象中拥有一个 ID "id": "0de616c939ce2f31676ff0294c78321b",在该要素的属性中拥有另一个 ID "id": "marker-iv1qi3x10"。
generateId我猜想,mapbox 在内部使用的功能 ID(以及在源中设置时自动生成的 ID true)是第一个。
假设我手动设置 ID:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id": "customPropId01"
},
"geometry": {
"coordinates": [
-87.603735,
41.829985
],
"type": "Point"
},
"id": "customID01"
}
]
}
Run Code Online (Sandbox Code Playgroud)
当源加载后检查数据时,我的自定义 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);
});
Run Code Online (Sandbox Code Playgroud)
但是,当我单击地图上的标记并记录它时,我的要素的 ID 属性已被删除,现在是undefined:
我没有使用我的输入源数据来列出我的标记,而是查看了querySourceFeatures,但这没有帮助,因为它只返回地图边界框中的功能 - 而且我希望我的列表显示所有功能,即这就是为什么我需要在那里使用“原始”源数据。
这真让我抓狂。有谁知道为什么 ID 未设置以及如何解决此问题?
谢谢 !
我发现您可以设置promoteId要用作 ID 的功能属性:
map.addSource('markers',{
type:"geojson",
data:"http://www.example.com/markers.geojson",
promoteId:'unique_id'
})
Run Code Online (Sandbox Code Playgroud)
使用 时promoteId:'unique_id',我的要素的属性unique_id将用于设置每个要素的 ID。
| 归档时间: |
|
| 查看次数: |
1723 次 |
| 最近记录: |