Leaflet Attribution

This is a fast tip post I wrote for myself so I can remember this situation. Leaflet makes easy to add attributions (usually) at the bottom right of the map. The docs show three methods:

  • setPrefix( prefix): Changes the Leaflet link to the desired one
  • addAttribution( text): Adds a new attribution text after the leaflet link. If called multiple times, the texts are separated by commas
  • removeAttribution( text): Removes the indicated attribution. You must know the text to remove

So what happens when you want to erase all the current attributions to add new ones? A use case could be when each layer must have different attributions. There are two options:

1.- Store the added attributions and call removeAttribution as many times as needed 2.- Reset the internal variable:

Looking at the code, there is a variable named _attributions, which is a key-value object. The keys are the attribution texts and the values the position numbers.

Removing all the attributions, then is as easy as:

leafletMap.attributionControl.\_attributions = {};
leafletMap.attributionControl.addAttribution("New attribution");