如何检查图层是否在用户视图中:openlayers

How to check if a layer is in the user's view: openlayers

我有一些功能只有在用户视野中时才需要显示,我该怎么做Image of a polygon that needs to be removed if it is off-screen of the user?

确定功能是否在视口中

var visibleExtent = map.getView().calculateExtent();

if (feature.getGeometry().intersectsExtent(visibleExtent)) {
  // feature is visible to user
}

请注意,如果旋转视图,可能会出现一些误报。可以通过测试旋转的几何形状和范围来消除它们,如本例 https://openlayers.org/en/latest/examples/box-selection.html