原文链接及内容

这个例子展示了如何添加一个永远不会从地图中消失的Attribution控件。它的工作原理是将静态字符串或HTML字符串传递到属性选项中,而属性选项没有链接到图层。点击“切换图层”按钮显示,即使没有图层,静态属性仍然在屏幕上。
main.js
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import Map from 'ol/Map.js'; import OSM from 'ol/source/OSM.js'; import TileLayer from 'ol/layer/Tile.js'; import View from 'ol/View.js'; import {Attribution, defaults as defaultControls} from 'ol/control.js';
const attribution = new Attribution({ collapsible: false, attributions: `<a href="https://openlayers.org">I'm a static attribution. I never disappear</a>`, }); const map = new Map({ layers: [ new TileLayer({ source: new OSM(), }), ], controls: defaultControls({attribution: false}).extend([attribution]), target: 'map', view: new View({ center: [0, 0], zoom: 2, }), });
document.getElementById('toggleLayerButton').addEventListener('click', () => { map.getLayers().forEach((l) => { l.setVisible(l.getVisible() ? false : true); }); });
|
界面布局文件index.html
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Static Attribution</title> <link rel="stylesheet" href="node_modules/ol/ol.css"> <style> .map { width: 100%; height: 400px; } </style> </head> <body> <div id="map" class="map"></div>
<button id="toggleLayerButton">Toggle layer</button>
<script type="module" src="main.js"></script> </body> </html>
|