Skip to content

Instantly share code, notes, and snippets.

@ajokela
Created October 14, 2011 21:00
Show Gist options
  • Save ajokela/1288325 to your computer and use it in GitHub Desktop.
Save ajokela/1288325 to your computer and use it in GitHub Desktop.
Is Point within Polygon
GeoHelper.IsInPolygon=function(points,latlong)
{
// This code adapted from the following URL:
// http://msdn.microsoft.com/en-us/library/cc451895.aspx
var i;
var j=points.length-1;
var inPoly=false;
var lat = latlong.Latitude;
var lon = latlong.Longitude;
for (i=0; i<points.length; i++)
{
if (points[i].Longitude<lon && points[j].Longitude>=lon || points[j].Longitude<lon && points[i].Longitude>=lon)
{
if (points[i].Latitude+(lon-points[i].Longitude)/(points[j].Longitude-points[i].Longitude)*(points[j].Latitude-points[i].Latitude)<lat)
{
inPoly=!inPoly;
}
}
j=i;
}
return inPoly;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment