I’m trying to test whether a Google Maps polyline passes through a Google Maps polygon. Sounds simple. But I’ve searched and searched... and found no real answers.
Closest I got was this function. It works but, frustratingly, returns the occasional false positive.
//nvert = the number of points in the polygon
//vertx = an array of all the polygon's latitudes
//verty = an array of all the polygon's longitudes
//elat = the current point's latitude
//elng = the current point's longitude
function pnpoly( nvert, vertx, verty, elat, elng) {
var i, j, c = false;
for( i = 0, j = nvert-1; i < nvert; j = i++ ) {
if( ( ( verty[i] > elng ) != ( verty[j] > elng ) ) &&
( elat < ( vertx[j] - vertx[i] ) * ( elng - verty[i] ) / ( verty[j] - verty[i] ) + vertx[i] ) ) {
c = !c;
}
}
return c;
}
Before I try a whole new method (a crazy math idea that brings me back to Grade 12 calculus), I’m wondering anyone knows how to accomplish this.