Reputation: 4676
I need to find if a geographical point is within a given feature. I need to find this before projection.
In my application I have a set of countries and I want to take the coordinates of the user and see which country those coordinates are within.
I am working through the lets make a map tutorial http://bost.ocks.org/mike/map/ and have code similar to the styling polygons section
Upvotes: 0
Views: 800
Reputation: 36
This might be a good starting point. From my bookmarks of using something similar a number of years ago to calculate point in polygon for Google Maps.
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/is-point-in-poly [rev. #0]
function isPointInPoly(poly, pt){
for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
&& (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
&& (c = !c);
return c;
}
Upvotes: 1