【发布时间】:2021-10-07 09:58:03
【问题描述】:
我有以下情况:
基础多边形是我定义应该绘制所有其他多边形的地方。
使用绘图交互时,设置条件选项足以限制点在地图上的放置位置(在我的例子中,是基础多边形)。
问题出在我修改它们的时候。我可以将一个顶点拖到基础多边形之外,我想禁用该行为,但我找不到任何解决方案。
如果我在修改交互上使用绘图交互中的相同条件选项,它只会在放置顶点后禁用拖动。
所以我的问题归结为禁用指定多边形外部的任何多边形的拖动。
编辑 1: 最后我得到了这个:
const constrainGeometry = (feature: RenderFeature | Feature<Geometry>) => {
const geometry = feature.getGeometry() as Polygon | undefined;
if (feature instanceof RenderFeature || geometry === undefined) {
return geometry;
}
const coordinates = geometry.getCoordinates()[0].map((coordinate) => {
const boundingGeometry = props.boundingFeature.getGeometry();
if (boundingGeometry) {
if (boundingGeometry.intersectsCoordinate(coordinate)) {
return coordinate;
} else {
return boundingGeometry.getClosestPoint(coordinate);
}
} else {
return coordinate;
}
});
return new Polygon([coordinates]);
};
尚未找到使用 TS 的良好解决方案,但目前看来还可以。
【问题讨论】:
标签: javascript openlayers polygon drag