How to get the area string from a polygon using leaflet.draw

后端 未结 4 1571
温柔的废话
温柔的废话 2020-12-18 21:58

I am trying to get the area measurements of polygons so I can list them in a table to the side of the map, next to the name of the polygon. This is what I have tried with n

4条回答
  •  一个人的身影
    2020-12-18 22:25

    I found that none of the above answers worked for calculating the area of non-contiguous polygons. Here's an example polygon where the above functions returned an area of 0:

    For anyone who needs to do that, here is the code that worked for me (using the L.GeometryUtil function from Leaflet.draw):

    var poly = // Your polygon layer here; may or may not be contiguous
    var area = 0;
    for (island of poly.getLatLngs()) {
        // If the polygon is non-contiguous, access the island
        if (island.length < 2) {
            island = island[0]
        }
        // Sum the area within each "island"
        area += L.GeometryUtil.geodesicArea(island);
    }

提交回复
热议问题