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

后端 未结 4 1575
温柔的废话
温柔的废话 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:14

    You can access the geometry utility library provided with Leaflet.

    var area = L.GeometryUtil.geodesicArea(layer.getLatLngs());
    

    In your example, you are trying to access a control itself, which is what the variable createPolygon is assigned to. Instead, you want to take the area of the layer that got drawn.

    map.on('draw:created', function (e) {
      var type = e.layerType,
          layer = e.layer;
      if (type === 'polygon') {
        polygons.addLayer(layer);
        var seeArea = L.GeometryUtil.geodesicArea(layer.getLatLngs());
        console.log(seeArea);
      }
    }
    

    Once you verify you are getting the area, you can just assign it to the variables that populate the table next to the map.

    Note: area will be in squareMeters by default

提交回复
热议问题