2

I use OpenLayer with the possibility to switch from two different base layers. So, I have different Attribution for each layer (as links) on the bottom-right of the map.

Is it possible to add one more Attribution (a link) on the bottom-left of the map? I need to have this Attribution shared between all layers (I don't need to change it when I switch layer)

Safari
  • 11,437
  • 24
  • 91
  • 191

1 Answers1

1

You could set the shared attribution label to a variable and then added it to the layers attributions. Check at this example I made for you by modifying one of OL's, in the example I use as the shared attribution the OSM attribution.

<!doctype html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.2.1/css/ol.css" type="text/css">
    <style>
      .map {
        height: 400px;
        width: 100%;
      }
    </style>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.2.1/build/ol.js"></script>
    <title>Attributions</title>
  </head>
  <body>
    <h2>Attributions</h2>
    <button onclick="switchMap()">Switch Map</button>
    <div id="map" class="map"></div>
    <script type="text/javascript">
      var osm = new ol.layer.Tile({
        source: new ol.source.OSM({
          attributions: [
            'All maps © <a href="https://www.opencyclemap.org/">OpenCycleMap</a>',
            ol.source.OSM.ATTRIBUTION
          ]
        })
      });
      var openSeaMapLayer = new ol.layer.Tile({
        source: new ol.source.OSM({
          attributions: [
            'All maps © <a href="http://www.openseamap.org/">OpenSeaMap</a>',
            ol.source.OSM.ATTRIBUTION
          ],
          opaque: false,
          url: 'https://tiles.openseamap.org/seamark/{z}/{x}/{y}.png'
        }),
        visible: false
      });
      var map = new ol.Map({
        target: 'map',
        layers: [
          osm,
          openSeaMapLayer
        ],
        view: new ol.View({
          center: [-244780.24508882355, 5986452.183179816],
          zoom: 18
        })
      });
      function switchMap() {
        openSeaMapLayer.setVisible(!openSeaMapLayer.getVisible());
        osm.setVisible(!osm.getVisible());
      };
    </script>
  </body>
</html>
cabesuon
  • 4,860
  • 2
  • 15
  • 24