Raruto / leaflet-rotate

Leaflet plugin that allows to add rotation functionality to map tiles
GNU General Public License v3.0
77 stars 21 forks source link

toggling leaflet.control.rotate breaks rotate functionality on map #23

Closed rphlo closed 1 year ago

rphlo commented 1 year ago

When adding the leaflet.control.rotate Control it add a listener on rotate map event

onAdd: function () {
  ...
  map.on('rotate', ...)
},

However when the control is removed the event listener is not removed from the map, and when calling map.setBearing or triggering the rotate event it triggers a JS error as the control does not exist anymore. Removing the event listener in onRemove function would solve this issue.

Raruto commented 1 year ago

@rphlo It looks like you're right, feel free to submit a pull request to fix it.

👋 Raruto

rphlo commented 1 year ago

I would do a pull request but I haven't found the right solution so far. Adding

onRemove: function(map) {
  map.off('rotate', this._restyle.bind(this));
}

doesn't work.

For my project I added:

onRemove: function(map) {
  map.off('rotate');
}

This work in my case, but would break functionalities for people who would have added their own event listener on 'rotate' event.

Raruto commented 1 year ago

If so, can you attach some jsfiddle (or similar) with the tests you've already done?

Just to speed up debugging a little bit ☺️.

rphlo commented 1 year ago

here https://jsfiddle.net/kp2wzgjd/1/