How do I prevent Leaflet concatenating attributions from multiple base layers?

Viewed 105

I have a fairly basic map using Leaflet, with two base layers that can be selected using the standard layer selection control. Each layer has its own attribution string, which is shown in the usual position at the bottom right. Here's a simplified version of the code I'm using, as a demo:

 <script>

    var zoomLevel = 6;
    var lat = 54.00366;
    var lon = -2.547855;
    
    var OSM = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            maxZoom: 18,
            attribution: 'this is the string for OSM, and should only show when OSM is selected'
    });

    var Esri_WorldImagery = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
            maxZoom: 18,    
            attribution: 'this is the string for Esri, and should only show when Esri is selected'
    });
    
    var baseMaps = {
        "Satellite": Esri_WorldImagery,
        "Open StreetMap": OSM
    
    };

    var map = L.map('map',{ 
        dragging: !L.Browser.mobile,
        center: [lat, lon],
        zoom: 6, 
        layers: [Esri_WorldImagery,OSM]
    });
    L.control.layers(baseMaps).addTo(map);
    
</script>

The problem I'm having is that when the map first loads, both attribition strings are concatenated and shown as a single string. As soon as either layer is actually selected, the only attribution string which is shown is the one which applies to that layer.

Here's a copy of the test map on my server:

https://markgoodge.com/demo/maptest.html

If you open that page, you will see the attribution string initially reads

"this is the string for Esri, and should only show when Esri is selected, this is the string for OSM, and should only show when OSM is selected"

which is incorrect - it should only show the OSM attribution on initial load, as the default base map is OSM.

Select either base map, and the string becomes the correct one for that layer.

How do I stop it doing that? How do I make the initial attribution string the correct one for the default layer?

(The reason this matters, is that it may not be so much of an issue when there are only two base maps, but when there are several - and I have a site which does use several - then the initial concatenated attribution string runs over several lines and takes up far too much space at the bottom of the map).

Edit: I've just spotted two things. Firstly, if the strings are identical, they don't get concatenated - it's only shown once. And, secondly, when they are concatenated, a comma is being inserted between them. So it's clearly deliberate behaviour by Leaflet, not a bug or gotcha. But, if so, there must, surely, be a setting to override it, as there are many occasions when this behaviour is inappropriate. Am I missing something in the documentation?

1 Answers

OK, figured it out. Sorry, should have experimented more before asking. The solutions is simple - only include a single (default) layer in the inital map options, That is:

layers: [Esri_WorldImagery,OSM]

should be

layers: [OSM]

Only load a single default layer, and only that layer's attribution is shown.

Related