How can I find out if a lat/lng exists inside a polygon? Javascript 4x.

388
1
Jump to solution
12-07-2021 09:26 AM
ScottAndersonGQ
New Contributor III

I have a polygon. I have a point. I want to know if the point is inside the polygon. 

rings == [[[lat, lng],[lat, lng]...]]

I've also tried: rings == [[lat, lng],[lat, lng],...]

My Polygon is created like:  var poly = new Polygon({ rings, spatialReference: { wkid: 4326 } });

My Point is created like:  var pointy = new Point({ latitude, longitude, spatialReference: { wkid: 4326 } });

 

poly.contains(pointy) always returns false. What am I doing wrong? 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor II

If the point exists on one of the vertices or along the edge of the polygon, then contains will return false, since it is not fully contained. You can try using intersects in the geometry engine to see if that will give you a better answer.

 

Here is a demo.

https://codepen.io/odoe/pen/bGopRPo?editors=0011

View solution in original post

1 Reply
ReneRubalcava
Frequent Contributor II

If the point exists on one of the vertices or along the edge of the polygon, then contains will return false, since it is not fully contained. You can try using intersects in the geometry engine to see if that will give you a better answer.

 

Here is a demo.

https://codepen.io/odoe/pen/bGopRPo?editors=0011