Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I searched in the OpenLayers API, but didn't find a solution.

screenshot for visual clarification

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?

like image 925
Abdelmoghite Kacimi Avatar asked Jan 03 '17 15:01

Abdelmoghite Kacimi


1 Answers

You have a method 'intersectsCoordinate' for the ol.geom.Geometry.

So the code for that will look like:

var polygonGeometry = e.feature.getGeometry();
var coords = iconFeature.getGeometry().getCoordinates();
polygonGeometry.intersectsCoordinate(coords)
like image 73
FatAl Avatar answered Sep 24 '22 16:09

FatAl