I created a polygon from ar2D.
var polygon_ = new esri.geometry.Polygon({
"rings": ar2D,
"spatialReference": {
"wkid": 102100
}
});
var extent = polygon_.getExtent();
console.log("polygonExtent", extent);
var bContains = polygon_.contains(new esri.geometry.Point( ar2D[0] ));
------------------
However, the Results show:
the values of extent.xmax,extent.xmin,extent.ymax,extent.ymin are NaN
bContains = false
Unknwon why? Thanks if you can help.
Solved! Go to Solution.
If ar2D[0] represents a point, then it wouldn't make sense to use ar2D as the 'rings' of a polygon. Considering that the extent coordinates are NaN, my guess is that ar2D needs to be wrapped up in an array like so:
var polygon_ = new esri.geometry.Polygon({
"rings": [ar2D],
"spatialReference": {
"wkid": 102100
}
});
...but just guessing as we have no idea what ar2D is...
Shaning,
Do your points in your array have a spatial reference assigned?
Vince Angelo asked a similar question about one of Shaning's other posts with no response yet
https://community.esri.com/thread/211013-polygoncontans-point-returns-false
If ar2D[0] represents a point, then it wouldn't make sense to use ar2D as the 'rings' of a polygon. Considering that the extent coordinates are NaN, my guess is that ar2D needs to be wrapped up in an array like so:
var polygon_ = new esri.geometry.Polygon({
"rings": [ar2D],
"spatialReference": {
"wkid": 102100
}
});
...but just guessing as we have no idea what ar2D is...
Revised the code as
var ar2D = new Array();
for (var j = 0; j < p_.length; j++) {
var ar1D = [p_
ar2D.push(ar1D);
} var polygon = new esri.geometry.Polygon(new esri.SpatialReference( { wkid: 102100 } ) );
polygon.addRing( ar2D );
var pt = new esri.geometry.Point(ar2D[0], new esri.SpatialReference({ wkid: 102100 }));
Then polygon.contains(pt) returns true.
Thanks to Robert, Dan and John.
Shaning,
Don't forget to mark this question as answered by clicking on the "Mark Correct" link on the reply that answered your question.