避免 queryRenderedFeatures 在不存在的图层上出错
Avoid queryRenderedFeatures erroring out on non existing layers
我已经将一个应用程序升级到最新版本的 mapbox-gl-js 但它崩溃了。
queryRenderedFeatures 已更改,现在它在不存在的层上出错。
由于多种原因,我们无法预测此时将存在哪些层(有些是动态构建的)。
有什么方法可以解决此问题?
基本上我们希望能够执行以下操作(其中一层不存在)并且仍然得到结果。
谢谢,
JM
features = map.queryRenderedFeatures([{
x: x1,
y: y1
}, {
x: x2,
y: y2
}], {
layers: [
'Layer A',
'Possibly non existing Layer B',
'Layer C'
]
});
您可以像这样过滤掉不存在的图层:
features = map.queryRenderedFeatures(
[{x: x1, y: y1}, {x: x2, y: y2}],
{layers:
['Layer A', 'Possibly non existing Layer B', 'Layer C']
.filter((layer)=>{map.getLayer(layer)})]
});
这应该可以避免触发错误
我已经将一个应用程序升级到最新版本的 mapbox-gl-js 但它崩溃了。
queryRenderedFeatures 已更改,现在它在不存在的层上出错。
由于多种原因,我们无法预测此时将存在哪些层(有些是动态构建的)。
有什么方法可以解决此问题?
基本上我们希望能够执行以下操作(其中一层不存在)并且仍然得到结果。
谢谢, JM
features = map.queryRenderedFeatures([{
x: x1,
y: y1
}, {
x: x2,
y: y2
}], {
layers: [
'Layer A',
'Possibly non existing Layer B',
'Layer C'
]
});
您可以像这样过滤掉不存在的图层:
features = map.queryRenderedFeatures(
[{x: x1, y: y1}, {x: x2, y: y2}],
{layers:
['Layer A', 'Possibly non existing Layer B', 'Layer C']
.filter((layer)=>{map.getLayer(layer)})]
});
这应该可以避免触发错误