Created
March 22, 2017 16:39
-
-
Save belal-mazlom/d130c57e2712922900b6dc32f372671a to your computer and use it in GitHub Desktop.
Determine if a location is within defined geographical boundaries
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
/* | |
============================================= | |
Determine if a location is within defined geographical boundaries | |
Using this alogrthim | |
http://alienryderflex.com/polygon/ | |
And also this post | |
http://stackoverflow.com/a/3036426/292927 | |
============================================= | |
*/ | |
/* | |
ex: | |
containsLocation({lat: 31.98958743167164, lng: 35.944175720214844} | |
, [{lat: 32.141897196425795, lng: 35.728912353515625}, | |
{lat: 32.12677940089151, lng: 36.18072509765625}, | |
{lat: 31.781882156411022, lng: 36.12579345703125}, | |
{lat: 31.804059692799363, lng: 35.66986083984375}]); | |
*/ | |
function containsLocation(point, paths) { | |
var j = 0; | |
var oddNodes = false; | |
var x = point.lng; | |
var y = point.lat; | |
for (var i = 0; i < paths.length; i++) { | |
j++; | |
if (j == paths.length) { | |
j = 0; | |
} | |
if (((paths[i].lat < y) && (paths[j].lat >= y)) | |
|| ((paths[j].lat < y) && (paths[i].lat >= y))) { | |
if (paths[i].lng + (y - paths[i].lat) | |
/ (paths[j].lat - paths[i].lat) | |
* (paths[j].lng - paths[i].lng) < x) { | |
oddNodes = !oddNodes | |
} | |
} | |
} | |
return oddNodes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment