Mapbox GL Js:添加和删除 GeoJSON 源和图层
Mapbox GL Js: adding and removing GeoJSON sources and layers
我在 mapbox gl 中添加和删除图层时遇到问题。
我有这一层:
map.addLayer({
"id": "route",
"type": "line",
"source": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-122.48369693756104, 37.83381888486939],
[-122.48348236083984, 37.83317489144141],
[-122.48339653015138, 37.83270036637107],
[-122.48356819152832, 37.832056363179625],
[-122.48404026031496, 37.83114119107971],
[-122.48404026031496, 37.83049717427869],
[-122.48348236083984, 37.829920943955045],
[-122.48356819152832, 37.82954808664175],
[-122.48507022857666, 37.82944639795659],
[-122.48610019683838, 37.82880236636284],
[-122.48695850372314, 37.82931081282506],
[-122.48700141906738, 37.83080223556934],
[-122.48751640319824, 37.83168351665737],
[-122.48803138732912, 37.832158048267786],
[-122.48888969421387, 37.83297152392784],
[-122.48987674713133, 37.83263257682617],
[-122.49043464660643, 37.832937629287755],
[-122.49125003814696, 37.832429207817725],
[-122.49163627624512, 37.832564787218985],
[-122.49223709106445, 37.83337825839438],
[-122.49378204345702, 37.83368330777276]
]
}
}
},
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": "#888",
"line-width": 8
}
});
然后我将其删除:
map.removeLayer('route')
一切正常。
然而,当我再次尝试添加同一层时,出现以下错误:
Error: There is already a source with this ID
尽管我正在添加一个图层。我不知道如何删除源,因为源没有 ID。
我的最终结果是能够通过单击按钮来添加和删除该图层。
有人可以帮我吗?
我刚刚发现源是使用与层中相同的 id 创建的,所以:
map.removeSource('route')
完美地完全删除图层和源。
您应该先删除图层,然后再删除其来源。
map.removeLayer(id);
map.removeSource(id);
你的情况 id = 'route'
.
...并在尝试删除
之前检查 source/layer 是否存在
if (map.getLayer(id)) {
map.removeLayer(id);
}
if (map.getSource(id)) {
map.removeSource(id);
}
我在 mapbox gl 中添加和删除图层时遇到问题。
我有这一层:
map.addLayer({
"id": "route",
"type": "line",
"source": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-122.48369693756104, 37.83381888486939],
[-122.48348236083984, 37.83317489144141],
[-122.48339653015138, 37.83270036637107],
[-122.48356819152832, 37.832056363179625],
[-122.48404026031496, 37.83114119107971],
[-122.48404026031496, 37.83049717427869],
[-122.48348236083984, 37.829920943955045],
[-122.48356819152832, 37.82954808664175],
[-122.48507022857666, 37.82944639795659],
[-122.48610019683838, 37.82880236636284],
[-122.48695850372314, 37.82931081282506],
[-122.48700141906738, 37.83080223556934],
[-122.48751640319824, 37.83168351665737],
[-122.48803138732912, 37.832158048267786],
[-122.48888969421387, 37.83297152392784],
[-122.48987674713133, 37.83263257682617],
[-122.49043464660643, 37.832937629287755],
[-122.49125003814696, 37.832429207817725],
[-122.49163627624512, 37.832564787218985],
[-122.49223709106445, 37.83337825839438],
[-122.49378204345702, 37.83368330777276]
]
}
}
},
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": "#888",
"line-width": 8
}
});
然后我将其删除:
map.removeLayer('route')
一切正常。
然而,当我再次尝试添加同一层时,出现以下错误:
Error: There is already a source with this ID
尽管我正在添加一个图层。我不知道如何删除源,因为源没有 ID。
我的最终结果是能够通过单击按钮来添加和删除该图层。
有人可以帮我吗?
我刚刚发现源是使用与层中相同的 id 创建的,所以:
map.removeSource('route')
完美地完全删除图层和源。
您应该先删除图层,然后再删除其来源。
map.removeLayer(id);
map.removeSource(id);
你的情况 id = 'route'
.
...并在尝试删除
之前检查 source/layer 是否存在if (map.getLayer(id)) {
map.removeLayer(id);
}
if (map.getSource(id)) {
map.removeSource(id);
}