0

In leaflet is there a way to change the attribution prefix when a specific baselayer change happens using the default layer control?

1

This is what I did, but I was hoping there was an option to set the layer's prefix on creation:

var baseLayers = {
  "Streets": streets,
  "Satellite": imagery,
  "Topographic": topo
};


map.on('baselayerchange', function (e) {
  if(e.name == 'Streets'){
    map.attributionControl.setPrefix('<a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a>');
  }else{
    map.attributionControl.setPrefix('<a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a> | Powered by <a href="https://www.esri.com">Esri</a>');
  }
});
0

For reference, the Leaflet Attribution Control concatenates its prefix and attributions from the layers currently added on map:

it fetches attribution texts from layers […] automatically.

Therefore, should you include your specific string in your layers' attribution, you could achieve an effect similar to what you have done using map.attributionControl.setPrefix:

var streets = L.tileLayer(urlTemplateStreets, {
  attribution: 'Some info'
});

var imagery = L.tileLayer(urlTemplateImagery, {
  attribution: 'Powered by (provider) | other info'
});
  • When only the streets layer is on map, the default attribution would display "Leaflet | Some info"
  • When it is replaced by imagery layer, the display would automatically change to "Leaflet | Powered by (provider) | other info"

Live example: https://plnkr.co/edit/81kncgMwXRJU7xR5jZAy?p=preview

  • Thank you for your feedback. I am using attributions when creating the layers. However, a plugin is setting the prefix when activated. I wanted to remove its prefix when layers that do not use the plugin are active. – lost.in.userspace Jul 18 '18 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.