1

I referred this post How to determine if a point is inside a 2D convex polygon?

But I want to do the same in OSM with Open Layers.Please help me.

[Link](http://jsfiddle.net/Sanju5390/3tpLs6w3/)
Community
  • 1
  • 1
Mindtek
  • 114
  • 1
  • 11
  • Actually OpenLayers 3 contains the algorithm to check if a point is inside a polygon ([ol.geom.Geometry.containsCoordinate](https://github.com/openlayers/ol3/blob/3595c2c/src/ol/geom/geometry.js#L132-L138)), but the method is not exported in the API. If you think it should be exported, feel free to open an issue: https://github.com/openlayers/ol3/issues – tsauerwein Sep 04 '15 at 07:06

1 Answers1

1

You can do this with turf.js using turf.inside:

var polygon = new ol.Feature(new ol.geom.Polygon([[[-5e6, -1e6], [-4e6, 1e6],
            [-3e6, -1e6], [-5e6, -1e6]]]));
var point = new ol.Feature(new ol.geom.Point([-4e6, 0e6]));

var format = new ol.format.GeoJSON();
var isInside = turf.inside(
    format.writeFeatureObject(point),
    format.writeFeatureObject(polygon));
console.log(isInside);

http://jsfiddle.net/d6o81vc7/22/

tsauerwein
  • 5,841
  • 3
  • 36
  • 49