Skip to content

Instantly share code, notes, and snippets.

@avin
Created May 15, 2015 08:54
Show Gist options
  • Save avin/5a2f9315f5551d138331 to your computer and use it in GitHub Desktop.
Save avin/5a2f9315f5551d138331 to your computer and use it in GitHub Desktop.
"Is point in polygon" function for leaflet
var isInPolygon = function (pointLanLng, polygon) {
var n, i, j, xi, xj, yi, yj, intersect,
inside = false,
x = pointLanLng.lat, y = pointLanLng.lng,
polygonLatLngs = polygon.getLatLngs();
for (n = 0; n < polygonLatLngs.length; n = n+1) {
for (i = 0, j = polygonLatLngs[n].length - 1; i < polygonLatLngs[n].length; j = i++) {
xi = polygonLatLngs[n][i]['lat'];
yi = polygonLatLngs[n][i]['lng'];
xj = polygonLatLngs[n][j]['lat'];
yj = polygonLatLngs[n][j]['lng'];
intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
}
return inside;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment