原文链接及内容

通过切换上述下拉框的下拉选项,可以改变比例尺的单位及样式。
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| 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 {ScaleLine, defaults as defaultControls} from 'ol/control.js';
const scaleBarOptionsContainer = document.getElementById('scaleBarOptions'); const unitsSelect = document.getElementById('units'); const typeSelect = document.getElementById('type'); const stepsRange = document.getElementById('steps'); const scaleTextCheckbox = document.getElementById('showScaleText'); const invertColorsCheckbox = document.getElementById('invertColors');
let control;
function scaleControl() { if (typeSelect.value === 'scaleline') { control = new ScaleLine({ units: unitsSelect.value, }); scaleBarOptionsContainer.style.display = 'none'; } else { control = new ScaleLine({ units: unitsSelect.value, bar: true, steps: parseInt(stepsRange.value, 10), text: scaleTextCheckbox.checked, minWidth: 140, }); onInvertColorsChange(); scaleBarOptionsContainer.style.display = 'block'; } return control; } const map = new Map({ controls: defaultControls().extend([scaleControl()]), layers: [ new TileLayer({ source: new OSM(), }), ], target: 'map', view: new View({ center: [0, 0], zoom: 2, }), });
function reconfigureScaleLine() { map.removeControl(control); map.addControl(scaleControl()); } function onChangeUnit() { control.setUnits(unitsSelect.value); } function onInvertColorsChange() { control.element.classList.toggle( 'ol-scale-bar-inverted', invertColorsCheckbox.checked ); } unitsSelect.addEventListener('change', onChangeUnit); typeSelect.addEventListener('change', reconfigureScaleLine); stepsRange.addEventListener('input', reconfigureScaleLine); scaleTextCheckbox.addEventListener('change', reconfigureScaleLine); invertColorsCheckbox.addEventListener('change', onInvertColorsChange);
|
界面布局文件index.html
代码如下:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Scale Line</title> <link rel="stylesheet" href="node_modules/ol/ol.css"> <style> .map { width: 100%; height: 400px; } #scaleBarOptions { display: none; }
input[type=range] { vertical-align: middle; }
.ol-scale-bar-inverted .ol-scale-singlebar-even { background-color: var(--ol-background-color); }
.ol-scale-bar-inverted .ol-scale-singlebar-odd { background-color: var(--ol-subtle-foreground-color); } </style> </head> <body> <div id="map" class="map"></div> <label for="units">Units:</label> <select id="units"> <option value="degrees">degrees</option> <option value="imperial">imperial inch</option> <option value="us">us inch</option> <option value="nautical">nautical mile</option> <option value="metric" selected>metric</option> </select>
<label for="type">Type:</label> <select id="type"> <option value="scaleline">ScaleLine</option> <option value="scalebar">ScaleBar</option> </select>
<div id="scaleBarOptions"> <label for="steps">Steps:</label> <input id="steps" type="range" value="4" min="1" max="8">
<label><input type="checkbox" id="showScaleText" checked> Show scale text</label>
<label><input type="checkbox" id="invertColors"> Invert colors</label> </div> <script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script> <script type="module" src="main.js"></script> </body> </html>
|