Mouse Position

mouse-position1 openstreetmap21

Example of a mouse position control, outside the map.

Example of a mouse position control, outside the map.

import 'ol/ol.css';
import Map from 'ol/Map';
import MousePosition from 'ol/control/MousePosition';
import OSM from 'ol/source/OSM';
import TileLayer from 'ol/layer/Tile';
import View from 'ol/View';
import {createStringXY} from 'ol/coordinate';
import {defaults as defaultControls} from 'ol/control';

const mousePositionControl = new MousePosition({
  coordinateFormat: createStringXY(4),
  projection: 'EPSG:4326',
  // comment the following two lines to have the mouse position
  // be placed within the map.
  className: 'custom-mouse-position',
  target: document.getElementById('mouse-position'),

const map = new Map({
  controls: defaultControls().extend([mousePositionControl]),
  layers: [
    new TileLayer({
      source: new OSM(),
  target: 'map',
  view: new View({
    center: [0, 0],
    zoom: 2,

const projectionSelect = document.getElementById('projection');
projectionSelect.addEventListener('change', function (event) {

const precisionInput = document.getElementById('precision');
precisionInput.addEventListener('change', function (event) {
  const format = createStringXY(event.target.valueAsNumber);
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Mouse Position</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>
      .map {
        width: 100%;
    <div id="map" class="map"></div>
    <div id="mouse-position"></div>
      <label for="projection">Projection </label>
      <select id="projection">
        <option value="EPSG:4326">EPSG:4326</option>
        <option value="EPSG:3857">EPSG:3857</option>
      <label for="precision">Precision</label>
      <input id="precision" type="number" min="0" max="12" value="4"/>
    <script src="main.js"></script>
  "name": "mouse-position",
  "dependencies": {
    "ol": "6.8.1"
  "devDependencies": {
    "parcel": "^2.0.0-beta.1"
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --public-url . index.html"