admin管理员组

文章数量:1191737

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.

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?

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.

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?

Share Improve this question edited Jan 10, 2017 at 10:08 ahocevar 5,64818 silver badges32 bronze badges asked Jan 3, 2017 at 15:02 Abdelmoghite KacimiAbdelmoghite Kacimi 1831 gold badge2 silver badges8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 24

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)

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.

See an example in 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.

本文标签: javascriptCheck if a point is inside polygon in OpenLayers 3Stack Overflow