Created
May 15, 2015 08:54
-
-
Save avin/5a2f9315f5551d138331 to your computer and use it in GitHub Desktop.
"Is point in polygon" function for leaflet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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