From 6f7de30b2be96a51cc0605727f22208d09c7cc2d Mon Sep 17 00:00:00 2001 From: Erick Simon Escalante Olano Date: Sat, 29 Apr 2023 08:06:21 -0500 Subject: [PATCH] Update latlon center of update poligon When editing a polygon, the center point changed, but when reducing it, it was no longer located in the center --- leaflet-measure-path.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/leaflet-measure-path.js b/leaflet-measure-path.js index 9477c8f..1f76669 100644 --- a/leaflet-measure-path.js +++ b/leaflet-measure-path.js @@ -268,7 +268,21 @@ formatDistance: formatDistance, formatArea: formatArea, + getCentroid(points) { + let sumLat = 0; + let sumLng = 0; + const numPoints = points.length; + + for (let i = 0; i < numPoints; i++) { + sumLat += points[i].lat; + sumLng += points[i].lng; + } + + const centroidLat = sumLat / numPoints; + const centroidLng = sumLng / numPoints; + return [centroidLat, centroidLng]; + }, updateMeasurements: function() { if (!this._measurementLayer) return this; @@ -324,7 +338,7 @@ if (isPolygon && options.showArea && latLngs.length > 2) { formatter = options.formatArea || L.bind(this.formatArea, this); var area = ringArea(latLngs); - L.marker.measurement(this.getBounds().getCenter(), + L.marker.measurement(this.getCentroid(latLngs), formatter(area), options.lang.totalArea, 0, options) .addTo(this._measurementLayer); }