Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to finish a measure over the top of a layer #8

Closed
ping-spike opened this issue Aug 18, 2020 · 8 comments
Closed

Not able to finish a measure over the top of a layer #8

ping-spike opened this issue Aug 18, 2020 · 8 comments

Comments

@ping-spike
Copy link

If you have any layers displayed on the map, you can start a measure to the left of the layer (eg. polygon) and end it beyond the polygon, but you can not end the measurement ON the polygon.

@ping-spike
Copy link
Author

Example of measure working ok if you go beyond a layer:

Screen Shot 2020-08-18 at 20 12 44

@ping-spike
Copy link
Author

But if you try to click on the layer / polygon area to end the measure, instead of ending the measure the info balloon appears for that map layer / polygon:

Screen Shot 2020-08-18 at 20 13 36

@ping-spike
Copy link
Author

Is there a way to disable clicks on visible layers when the measure tool is turned on, then re-enable clicks on the layers when it is turned off again? 🤔

@aprilandjan
Copy link
Owner

@ping-spike hi, thanks for your feedback!

It seems that once we add popup to the layer/polygon, the click on it will be captured first and stops further event propagation, thus makes the measure layer cannot get the click event to draw line (Leaflet/Leaflet#765).

How ever, we can bypass that though some tricky methods. For example, when start measuring, loop through all layers and remove their click event handlers to prevent them from responding to click; and then, after finish measuring, we add back their click event handlers to keep its original behavior.

@ping-spike
Copy link
Author

Agreed, sounds tricky 😬

We almost need an interactive: false being applied to each active layer 🤔

Any pointers that I could experiment with?

@aprilandjan
Copy link
Owner

aprilandjan commented Aug 19, 2020

I wrote some experimental code in branch [feat/disable-other-clicks-while-measuring] (https://github.com/aprilandjan/leaflet.measure/tree/feat/disable-other-clicks-while-measuring), add an option disableOtherClicksWhileMeasuring to implement that, please have a try.

Here's the tricky:

  //  the cached clicks for other layers
  _cachedClicks: [],

  //  try to remove click event handlers of other layers
  _removeOtherLayersClicks: function (layer) {
    if (layer === this._layerPaint) {
      return;
    }
    var loop = function (childLayer) {
      if (childLayer._events && childLayer._events.click) {
        this._cachedClicks.push({
          layer: childLayer,
          click: childLayer._events.click,
        });
        childLayer._events.click = undefined;
      }
      if (childLayer.eachLayer) {
        this._removeOtherLayersClicks(childLayer);
      }
    }.bind(this);
    layer.eachLayer(loop);
  },

  //  try to bring back the click event handlers
  _addOtherLayersClicks: function () {
    this._cachedClicks.forEach(function (cached) {
      var layer = cached.layer;
      var click = cached.click;
      layer._events.click = click;
    });
  },

@ping-spike
Copy link
Author

Amazing work!

Tested and seems to work perfectly 👏

If I'm mid-measure and zoom the map in or out it seems to re-enable the layer clickability again. But hey, that's a minor.

This is fantastic, thanks for the seriously quick response times :)

@aprilandjan
Copy link
Owner

@ping-spike you are welcome 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants