Edit

Scale Line

Example of a scale line.

index.html<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Scale Line</title>
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></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">
      <input type="checkbox" id="showScaleText" checked>Show scale text
    </div>
    <script src="index.js"></script>
  </body>
</html>
index.jsimport 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import {defaults as defaultControls, ScaleLine} from 'ol/control';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';

var unitsSelect = document.getElementById('units');
var typeSelect = document.getElementById('type');
var stepsSelect = document.getElementById('steps');
var scaleTextCheckbox = document.getElementById('showScaleText');
var showScaleTextDiv = document.getElementById('showScaleTextDiv');

var scaleType = 'scaleline';
var scaleBarSteps = 4;
var scaleBarText = true;
var 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;
}
var 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);
package.json{
  "name": "scale-line",
  "dependencies": {
    "ol": "6.1.1"
  },
  "devDependencies": {
    "parcel": "1.11.0"
  },
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --experimental-scope-hoisting --public-url . index.html"
  }
}