Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
Find Point in polygon javascript

I found this solution in php : Find Point in polygon PHP

I change it in javascript

if (is_in_polygon(points_polygon, vertices_x, vertices_y, longitude_x, latitude_y)){
   console.log("Is in polygon");
}
else console.log("Is not  in polygon");

 function is_in_polygon(points_polygon, vertices_x, vertices_y, longitude_x, latitude_y) {
            i = j = c = 0;
            for (var i = 0, j = points_polygon - 1; i < points_polygon; j = i++) {
               if (((vertices_y[i] > latitude_y != (vertices_y[j] > latitude_y)) &&
                     (longitude_x < (vertices_x[j] - vertices_x[i]) * (latitude_y - vertices_y[i]) / (vertices_y[j] - vertices_y[i]) + vertices_x[i])))
                  c = !c;
            }
            return c;
         }

For test using this values :

var vertices_x = [37.628134, 37.629867, 37.62324, 37.622424]; 

 var vertices_y = [-77.458334,-77.449021,-77.445416,-77.457819];

 var points_polygon = vertices_x.length;

var longitude_x = 37.62850;
var latitude_y = -77.4499;

The function returned "Is in polygon"

Test 2 : I got points from drawing polygon leaflet map

var vertices_x = [49.209972,49.087808,48.231534,48.377689];

 var vertices_y = [1.658936, 3.131104, 3.35083, 1.647949];

 var points_polygon = vertices_x.length;

longitude_x = 2.3485;
latitude_y = 48.8537;

The point is inside polygon visually But the function returned "Is not in polygon"

I don't find what the problem .Any idea please for this problem

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!