Example of a scale line.
import 'ol/ol.css';
import Map from 'ol/Map';
import OSM from 'ol/source/OSM';
import TileLayer from 'ol/layer/Tile';
import View from 'ol/View';
import {ScaleLine, defaults as defaultControls} from 'ol/control';
const unitsSelect = document.getElementById('units');
const typeSelect = document.getElementById('type');
const stepsSelect = document.getElementById('steps');
const scaleTextCheckbox = document.getElementById('showScaleText');
const showScaleTextDiv = document.getElementById('showScaleTextDiv');
let scaleType = 'scaleline';
let scaleBarSteps = 4;
let scaleBarText = true;
let control;
function scaleControl() {
if (scaleType === 'scaleline') {
control = new ScaleLine({
units: unitsSelect.value,
});
return control;
}
control = new ScaleLine({
units: unitsSelect.value,
bar: true,
steps: scaleBarSteps,
text: scaleBarText,
minWidth: 140,
});
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 onChange() {
control.setUnits(unitsSelect.value);
}
function onChangeType() {
scaleType = typeSelect.value;
if (typeSelect.value === 'scalebar') {
stepsSelect.style.display = 'inline';
showScaleTextDiv.style.display = 'inline';
map.removeControl(control);
map.addControl(scaleControl());
} else {
stepsSelect.style.display = 'none';
showScaleTextDiv.style.display = 'none';
map.removeControl(control);
map.addControl(scaleControl());
}
}
function onChangeSteps() {
scaleBarSteps = parseInt(stepsSelect.value, 10);
map.removeControl(control);
map.addControl(scaleControl());
}
function onChangeScaleText() {
scaleBarText = scaleTextCheckbox.checked;
map.removeControl(control);
map.addControl(scaleControl());
}
unitsSelect.addEventListener('change', onChange);
typeSelect.addEventListener('change', onChangeType);
stepsSelect.addEventListener('change', onChangeSteps);
scaleTextCheckbox.addEventListener('change', onChangeScaleText);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Scale Line</title>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://unpkg.com/elm-pep"></script>
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v3/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,URL,TextDecoder,Number.isInteger"></script>
<style>
.map {
width: 100%;
height:400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<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>
<select id="type">
<option value="scaleline">ScaleLine</option>
<option value="scalebar">ScaleBar</option>
</select>
<select id="steps" style="display:none">
<option value=2>2 steps</option>
<option value=4 selected>4 steps</option>
<option value=6>6 steps</option>
<option value=8>8 steps</option>
</select>
<div id="showScaleTextDiv" style="display:none">
<label><input type="checkbox" id="showScaleText" checked> Show scale text</label>
</div>
<script src="main.js"></script>
</body>
</html>
{
"name": "scale-line",
"dependencies": {
"ol": "6.6.1"
},
"devDependencies": {
"parcel": "^2.0.0-beta.1"
},
"scripts": {
"start": "parcel index.html",
"build": "parcel build --public-url . index.html"
}
}