[英]Check if a point is inside polygon in OpenLayers 3
When I draw a polygon in an OpenLayers map, I want to know if the marker is inside the polygon or not. 当我在OpenLayers地图中绘制多边形时,我想知道标记是否在多边形内部。 I searched in the OpenLayers API, but didn't find a solution. 我在OpenLayers API中搜索过,但没有找到解决方案。
And you can see my full code in this link . 你可以在这个链接中看到我的完整代码。
I have the impression that I have to modify this function: 我的印象是我必须修改此功能:
function addInteraction() {
var value = typeSelect.value;
if (value !== 'None') {
draw = new ol.interaction.Draw({
source: vectorSource,
type: /** @type {ol.geom.GeometryType} */ (typeSelect.value)
});
map.addInteraction(draw);
draw.on('drawend',function(e){
//Here
});
}
}
How can I do this? 我怎样才能做到这一点?
You have a method ' intersectsCoordinate ' for the ol.geom.Polygon. 你有一个方法' intersectsCoordinate '为ol.geom.Polygon。
So the code for that will look like: 所以代码看起来像:
var polygonGeometry = e.feature.getGeometry();
var coords = iconFeature.getGeometry().getCoordinates();
polygonGeometry.intersectsCoordinate(coords)
You can use the JSTS library, which implements simple geometry processing such as intersects
, difference
, etc. It contains an OL3 parser that allows the conversion of geometry from OL3 to JSTS and vice-versa. 您可以使用JSTS库,它实现简单的几何处理,如intersects
, difference
等。它包含一个OL3解析器,允许将几何从OL3转换为JSTS,反之亦然。
See an example in OL3 . 请参阅OL3中的示例 。 Basically, you would use a process that checks if the geometry of your marker is within your polygon or not and do what you want from there. 基本上,您将使用一个过程来检查标记的几何形状是否在您的多边形内,并从那里做您想做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.