如何使用 forEachFeatureAtPixel 方法过滤图层
How to filter layers using forEachFeatureAtPixel method
我很难弄清楚如何使用 forEachFeatureAtPixel 方法过滤图层。我正在浏览文档,但到目前为止没有任何成功。我基本上想过滤图层并在事件上应用叠加样式(例如 "click"),或者更准确地说,我想使用此 example 实现悬停效果,但使用隔离层。
在上面的例子中是这样使用来获取特征的:
var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
return feature;
});
我想通过使用图层过滤器稍微调整一下代码,但我遇到了 syntax Uncaught SyntaxError: Unexpected token (
语法错误:
var features = map.getFeaturesAtPixel(pixel, function(features) {
layerFilter: function(layer) {
return layer.get('layer_name') === 'someName';
}
});
然后,我就这样试了
var feature = map.forEachFeatureAtPixel(pixel, {
layerFilter: function(layer) {
return layer.get('layer_name') === 'someName';
}
});
但后来我得到 Uncaught TypeError: d.call is not a function
错误
我正在使用文档,但公平地说,我在阅读和实施一些方法时遇到了一些困难 API
好的,我终于做到了。我在阅读文档时有点仓促,问题出在回调函数中。正如文档中指出的那样,我需要 return 功能。 ...要停止检测,回调函数可以return一个真值。
所以,正确的格式是这样的:(我使用的是4.5.6版本)
var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
return feature;
}, {
layerFilter: function(layer) {
return layer.get('layer_name') === 'someName';
}
});
现在工作正常。享受:)
对于那些像我一样受图层名称困扰的人,我可以建议您使用下面的替代方法;
我已将所有图层存储在变量中,然后像这样过滤它们;
var countryLayer = new VectorLayer({
source: countryLayerSource,
style: //some styling in here
});
...
var feature = map.forEachFeatureAtPixel(
pixel,
function (feature) {
return feature;
},
{
layerFilter: function (layer) {
return layer === countryLayer;
},
}
);
已使用 API:v6.3.1。 Openlayers API
我很难弄清楚如何使用 forEachFeatureAtPixel 方法过滤图层。我正在浏览文档,但到目前为止没有任何成功。我基本上想过滤图层并在事件上应用叠加样式(例如 "click"),或者更准确地说,我想使用此 example 实现悬停效果,但使用隔离层。
在上面的例子中是这样使用来获取特征的:
var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
return feature;
});
我想通过使用图层过滤器稍微调整一下代码,但我遇到了 syntax Uncaught SyntaxError: Unexpected token (
语法错误:
var features = map.getFeaturesAtPixel(pixel, function(features) {
layerFilter: function(layer) {
return layer.get('layer_name') === 'someName';
}
});
然后,我就这样试了
var feature = map.forEachFeatureAtPixel(pixel, {
layerFilter: function(layer) {
return layer.get('layer_name') === 'someName';
}
});
但后来我得到 Uncaught TypeError: d.call is not a function
错误
我正在使用文档,但公平地说,我在阅读和实施一些方法时遇到了一些困难 API
好的,我终于做到了。我在阅读文档时有点仓促,问题出在回调函数中。正如文档中指出的那样,我需要 return 功能。 ...要停止检测,回调函数可以return一个真值。
所以,正确的格式是这样的:(我使用的是4.5.6版本)
var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
return feature;
}, {
layerFilter: function(layer) {
return layer.get('layer_name') === 'someName';
}
});
现在工作正常。享受:)
对于那些像我一样受图层名称困扰的人,我可以建议您使用下面的替代方法;
我已将所有图层存储在变量中,然后像这样过滤它们;
var countryLayer = new VectorLayer({
source: countryLayerSource,
style: //some styling in here
});
...
var feature = map.forEachFeatureAtPixel(
pixel,
function (feature) {
return feature;
},
{
layerFilter: function (layer) {
return layer === countryLayer;
},
}
);
已使用 API:v6.3.1。 Openlayers API