Check if a point is inside a polygon in OpenLayers 3

When I draw a polyangular box on an OpenLayers map, I want to know if the marker is inside the polyangular box or not. I searched in the OpenLayers API but couldn't find a solution.

screenshot for visual clarification

And you can see my full code in this link .

I got the impression that I should change 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?

+9
source share
2 answers

You have an intersectsCoordinate method for ol.geom.Geometry.

So the code for this would look like this:

 var polygonGeometry = e.feature.getGeometry(); var coords = iconFeature.getGeometry().getCoordinates(); polygonGeometry.intersectsCoordinate(coords) 
+13
source

You can use the JSTS library that implements simple geometry such as intersects , difference , etc. It contains an OL3 analyzer that allows you to convert geometry from OL3 to JSTS and vice versa.

See an example in OL3 . Basically, you would use a process that checks if the geometry of your marker is inside your polygon or not, and do what you want from there.

+2
source

Source: https://habr.com/ru/post/1262225/


All Articles