We want to find whether a Point lies inside a Polygon.

var point = new esri.geometry.Point(-85.91,25.68,this.parent.esriMap.spatialReference);    

var polygon = new esri.geometry.Polygon(this.parent.esriMap.spatialReference);

polygon .addRing([contains the lat and long]);

boolean result=polygon .contains(point);   

The moment I add the contains method call (polygon .contains(point);), I get an error stating that ESRIMapViewer does not have a constructor. ESRIMapViewer.js is the file which contains the above code.


i've posted a working sample here

var polygon = new Polygon(new SpatialReference({wkid:4326}));

var point = new Point(-115, 35, {"spatialReference":{"wkid":4326 }})
polygon.contains(point); //returns true

are you sure that the SpatialReference you are digging out of your map is WGS84 and not WebMercator?

  • Thanks a lot for the pointer.There was one mistake in my code.boolean result=..... I changed it to var(it does accept the boolean data type) and also gave the proper spatial reference.Now I have another problem.When I hard code the lat and long as below it works polygon.addRing([[-120,30],[-120,40],[-110,40],[-110,30],[-120,30]]); When I copy the lat and long to a variable,the contains method returns false. var val="[[-120,30],[-120,40],[-110,40],[-110,30],[-120,30]]";polygon.addRing(val) – user39216 Oct 20 '14 at 5:35
  • your variable should be a real array, not a string, so drop the trailing and leading quotes. – John Gravois Oct 21 '14 at 18:28
  • Thanks John.I removed the trailing and leading quotes and it worked.Thanks a ton. – user39216 Oct 22 '14 at 8:35
  • Hi John Thanks a lot for your time and for your repiles.I have run into another issue.I get the latitude and longitude from two different entities.Hence I concatenate them as below var greenArr=new Array();var greenPolygon = new esri.geometry.Polygon(this.parent.esriMap.spatialReference); if (type == 'Green') {var coords = '[' + parseFloat(longitude)+ ','+ parseFloat(latitude)+ ']'; greenArr += coords + ","; } greenArr="["+ greenArr +"]"; greenPolygon.addRing(greenArr); Here the contains method is not working.it returns false always – user39216 Oct 22 '14 at 9:08
  • please give me credit for answering your original question and post this new question in a seperate thread with your code wrapped in the appropriate tags and I will take a look. – John Gravois Oct 22 '14 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.