1

我使用 OpenLayer 可以从两个不同的基础层切换。所以,我在地图右下角的每一层(作为链接)都有不同的属性。

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

4

1 回答 1

0

您可以将共享属性标签设置为变量,然后将其添加到图层属性中。检查我通过修改一个 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>

于 2020-03-13T17:37:01.977 回答