0

Check Latitude & Longitude is within Area without Google Maps Api

I make Boundary Polygon shape,

11.028321745643366, 76.92803098305058
11.032091713467828, 76.93215085609745
11.027795209976349, 76.93659259422611
11.024404297691508, 76.93137837990116

How can I check if the given (Lat, Lng) 11.028367, 76.932336 is inside that Boundary Polygon?

I need to do it without Google Maps API, because the program will be offline and must check more often than Google API’s free service will allow.

NodeJs function to detect Point in Polygon:

checkGeoFence(point, polygon)
{
    if(polygon[0] != polygon[polygon.length-1])
        polygon[polygon.length] = polygon[0];
    let j = 0;
    let oddNodes = false;
    let x = point[1];
    let y = point[0];
    let n = polygon.length;
    for (let i = 0; i < n; i++)
    {
        j++;
        if (j == n)
        {
            j = 0;
        }
        if (((polygon[i][0] < y) && (polygon[j][0] >= y)) || ((polygon[j][0] < y) && (polygon[i][0] >=
            y)))
        {
            if (polygon[i][1] + (y - polygon[i][0]) / (polygon[j][0] - polygon[i][0]) * (polygon[j][1] -
                polygon[i][1]) < x)
            {
                oddNodes = !oddNodes;
            }
        }
    }
    return oddNodes;
}

Test for convex polygon:

let polygon = [
    [0,0],
    [0,5],
    [4,3],
    [3,0],
    [0,0],
];
 
let point1 = [3,3];
result = checkGeoFence(point1,polygon)?'IN':'OUT';
console.log(result);

let point2 = [4,4];
result = checkGeoFence(point2,polygon)?'IN':'OUT';
console.log(result);

Result:

IN
OUT

Test for concave polygon:

let polygon = [
    [0,0],
    [0,5],
    [2,2],
    [4,3],
    [4,0],
    [2,1],
    [0,0],
];
  
let point1 = [1,4];
result = checkGeoFence(point1,polygon)?'IN':'OUT';
console.log(result);
  
let point2 = [3,2];
result = checkGeoFence(point2,polygon)?'IN':'OUT';
console.log(result);
  
let point3 = [2,0.5];
result = checkGeoFence(point3,polygon)?'IN':'OUT';
console.log(result);

Result:

OUT
IN
OUT

Prakash S

Prakash S

I would like to introduce myself as a Software professional opting for the career in software industry. I'm Prakash S, a MCA graduate and trained as industry level practice for Software technology. Basically I am a PHP Developer but now days exploring more in HTML5, CSS, AngularJS and jQuery libraries.