Get area from Latitude/Longitude Point

514 Views Asked by At

I have an leaflet map where the users draw a polygon, and this polygon gave me lat and long points.

I'm using react js too.

polygon draw

i would like to know the polygon area so that i can take the id from the black dots that are within the polygon area. So the question is:

how do I remove the area of ​​my polygon according to the information generated from it?

values from polygon drawn

polygon draw function


        if (type === 'Polygon') {
          console.log(layer);
          var geojson = layer.toGeoJSON();
          console.log(geojson);
        }
1

There are 1 best solutions below

0
readyplayer77 On

I'm assuming that finding the area of a polygon would resolve your problem. To find an area of the polygon, try using the shoelace algorithm. Learn more about how the algorithm works here

// shoelace algorithm
function polygonArea(coordinates) {
    var sum = 0.0;
    var length = coordinates.length;
    if (length < 3) {
        return sum;
    }
    coordinates.forEach(function (d1, i1) {
        var i2 = (i1 + 1) % length;
        var d2 = coordinates[i2];
        sum += (d2[1] * d1[0]) - (d1[1] * d2[0]);
    });
    return sum / 2;
}