getFeaturesAtPixel() 以包括整理(隐藏)功能
getFeaturesAtPixel() to include decluttered (hidden) features
有没有办法在一个特定像素的一层中获取所有特征,包括由于整理而隐藏的特征?目前,当调用 Map.getFeaturesAtPixel()
(或 Map.forEachFeatureAtPixel()
)时,这些功能将被忽略。
getFeaturesAtPixel
旨在准确报告地图上呈现的内容。如果你想获得特定位置的所有功能,你可以使用 ol/source/Vector
的 getFeaturesInExtent
方法在你感兴趣的坐标周围的一个小缓冲区(例如 2 像素):
import {boundingExtent, buffer} from 'ol/extent';
map.on('click', function(e) {
const extent = boundingExtent([e.coordinate]);
buffer(extent, 2 / view.getResolution());
matches = source.getFeaturesInExtent(extent);
});
当您使用矢量切片时,您可以通过先获取切片来实现同样的效果
const tileGrid = vectorTileSource.getTileGrid();
const tileCoord = tileGrid.getTileCoordForCoordAndResolution(coordinate, view.getResolution());
const tile = vectorTileSource.getTile(tileCoord);
然后只获取缓冲区范围内的特征:
import {intersects} from 'ol/extent';
const features = tile.getFeatures();
const matches = [];
for (let i = 0, ii = features.length; i < ii; ++i) {
const feature = features[i];
if (intersects(extent, feature.getGeometry().getExtent()) {
matches.push(feature);
}
}
为子孙后代。我认为在大多数情况下,由于整理,您不需要包含隐藏功能的结果,因为这可能会导致光标位于空白区域的非空结果。
最后做的是在不打开整理的情况下创建一个附加层。首先,我在其中添加了所有没有标签的功能,并简单地隐藏它们而不设置填充样式(将图层不透明度设置为零也可以)。当原始的整理特征与其他特征重叠时,这给了我很好的结果,但在空白区域仍然给出误报。
所以最后我决定在整理后的图层后面也显示这个新图层,它具有不同的样式且没有标签。通过这种方式,您可以直观地看到所有功能和标签的整理显示在顶部,从用户体验的角度来看也非常好。
有没有办法在一个特定像素的一层中获取所有特征,包括由于整理而隐藏的特征?目前,当调用 Map.getFeaturesAtPixel()
(或 Map.forEachFeatureAtPixel()
)时,这些功能将被忽略。
getFeaturesAtPixel
旨在准确报告地图上呈现的内容。如果你想获得特定位置的所有功能,你可以使用 ol/source/Vector
的 getFeaturesInExtent
方法在你感兴趣的坐标周围的一个小缓冲区(例如 2 像素):
import {boundingExtent, buffer} from 'ol/extent';
map.on('click', function(e) {
const extent = boundingExtent([e.coordinate]);
buffer(extent, 2 / view.getResolution());
matches = source.getFeaturesInExtent(extent);
});
当您使用矢量切片时,您可以通过先获取切片来实现同样的效果
const tileGrid = vectorTileSource.getTileGrid();
const tileCoord = tileGrid.getTileCoordForCoordAndResolution(coordinate, view.getResolution());
const tile = vectorTileSource.getTile(tileCoord);
然后只获取缓冲区范围内的特征:
import {intersects} from 'ol/extent';
const features = tile.getFeatures();
const matches = [];
for (let i = 0, ii = features.length; i < ii; ++i) {
const feature = features[i];
if (intersects(extent, feature.getGeometry().getExtent()) {
matches.push(feature);
}
}
为子孙后代。我认为在大多数情况下,由于整理,您不需要包含隐藏功能的结果,因为这可能会导致光标位于空白区域的非空结果。
最后做的是在不打开整理的情况下创建一个附加层。首先,我在其中添加了所有没有标签的功能,并简单地隐藏它们而不设置填充样式(将图层不透明度设置为零也可以)。当原始的整理特征与其他特征重叠时,这给了我很好的结果,但在空白区域仍然给出误报。
所以最后我决定在整理后的图层后面也显示这个新图层,它具有不同的样式且没有标签。通过这种方式,您可以直观地看到所有功能和标签的整理显示在顶部,从用户体验的角度来看也非常好。