From 2c38968798a6d0b5bd0d3c4afa82a5e0d1a453eb Mon Sep 17 00:00:00 2001 From: Manuel Huber Date: Mon, 26 Apr 2021 11:12:50 +0200 Subject: [PATCH 1/3] Prevent link from changing url hash --- src/leaflet.measure.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/leaflet.measure.js b/src/leaflet.measure.js index 6bd0aea..91fd728 100644 --- a/src/leaflet.measure.js +++ b/src/leaflet.measure.js @@ -89,6 +89,7 @@ }, _enableMeasureLine: function (ev) { L.DomEvent.stopPropagation(ev); + L.DomEvent.preventDefault(ev); this._measureHandler = new L.MeasureAction(this._map, { model: "distance", color: this.options.color, @@ -97,6 +98,7 @@ }, _enableMeasureArea: function (ev) { L.DomEvent.stopPropagation(ev); + L.DomEvent.preventDefault(ev); this._measureHandler = new L.MeasureAction(this._map, { model: "area", color: this.options.color, From 0fab156a5fb61e12c277b064134ffc8c4fe5a313 Mon Sep 17 00:00:00 2001 From: Manuel Huber Date: Tue, 26 Oct 2021 08:12:16 +0200 Subject: [PATCH 2/3] simplify layer add & removal --- src/leaflet.measure.js | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/leaflet.measure.js b/src/leaflet.measure.js index 91fd728..eae0a03 100644 --- a/src/leaflet.measure.js +++ b/src/leaflet.measure.js @@ -267,11 +267,13 @@ } }, _enableMeasure: function () { + var map = this._map; this._trail = { - overlays: [], points: [], + overlays: L.featureGroup(), }; - var map = this._map; + map.addLayer( this._trail.overlays ); + L.DomUtil.addClass(map._container, "leaflet-measure-map"); map.contextMenu && map.contextMenu.disable(); this._measurementStarted = true; @@ -337,8 +339,7 @@ interactive: false, }); } - this._map.addLayer(this._directPath); - this._trail.overlays.push(this._directPath); + this._trail.overlays.addLayer(this._directPath); } else { this._directPath.addLatLng(latlng); } @@ -360,8 +361,7 @@ interactive: false, }); } - this._map.addLayer(this._measurePath); - this._trail.overlays.push(this._measurePath); + this._trail.overlays.addLayer(this._measurePath); } else { this._measurePath.addLatLng(latlng); } @@ -378,8 +378,7 @@ radius: 3, interactive: false, }); - this._map.addLayer(marker); - this._trail.overlays.push(marker); + this._trail.overlays.addLayer(marker); }, _addLable: function (latlng, content, className, ended) { var lable = new L.MeasureLable({ @@ -387,20 +386,15 @@ content: content, className: className, }); - this._map.addLayer(lable); - this._trail.overlays.push(lable); + this._trail.overlays.addLayer(lable); if (ended) { var closeButton = lable.enableClose(); L.DomEvent.on(closeButton, "click", this._clearOverlay, this); } }, _clearOverlay: function () { - var i = 0, - overlays = this._trail.overlays, - length; - for (length = overlays.length; i < length; i++) { - this._map.removeLayer(overlays[i]); - } + this._map.removeLayer(this._trail.overlays); + this._trail.overlays = null; }, toRadians: function (deg) { return deg * (Math.PI / 180); From 1fa58abb113de9aed60e524f73ea11433c5b6f14 Mon Sep 17 00:00:00 2001 From: Manuel Huber Date: Tue, 26 Oct 2021 08:25:28 +0200 Subject: [PATCH 3/3] Add workaround for maps with preferCanvas=true --- src/leaflet.measure.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/leaflet.measure.js b/src/leaflet.measure.js index eae0a03..570cf55 100644 --- a/src/leaflet.measure.js +++ b/src/leaflet.measure.js @@ -271,7 +271,14 @@ this._trail = { points: [], overlays: L.featureGroup(), + canvas: map.options.preferCanvas || false, }; + if ( map.options.preferCanvas ) { + map.options.preferCanvas = false; + console.warn( 'Temporarily reset map.options.prefersCanvas to false' ); + //HACK: With canvas rendering enabled (and no other markers present on the map), this will create an permanent + // overlaying layer of type L.Canvas that swallows mouse events. + } map.addLayer( this._trail.overlays ); L.DomUtil.addClass(map._container, "leaflet-measure-map"); @@ -395,6 +402,7 @@ _clearOverlay: function () { this._map.removeLayer(this._trail.overlays); this._trail.overlays = null; + this._map.options.preferCanvas = this._trail.canvas; }, toRadians: function (deg) { return deg * (Math.PI / 180);