我使用OpenLayer,可以从两个不同的基本层进行切换。
因此,地图右下角的每个图层(作为链接)的归因都不同。

是否可以在地图的左下角再添加一个归因(链接)?
我需要在所有图层之间共享此归因(切换图层时无需更改)

最佳答案

您可以将共享属性标签设置为变量,然后将其添加到图层属性。在此示例中,通过修改一个OL来检查为您提供的示例,在该示例中,我将OSM属性用作共享属性。



<!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>

08-17 12:50