O'Reilly logo

OpenLayers 3 : Beginner's Guide by Erik Hazzard, Paul Spencer, Thomas Gratier

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – finding your mouse position

Follow these steps to get started with the mouse position behavior:

  1. Reuse the usual HTML for the chapter by copying it in a new file.
  2. Now, edit the file to be sure that you create an HTML element with an ID myposition such as <div id="myposition"></div>.
  3. Declare a JavaScript variable that references the control:
    var mousePosition = new ol.control.MousePosition({
      coordinateFormat: ol.coordinate.createStringXY(2),
      projection: 'EPSG:4326',
      target: document.getElementById('myposition'),
      undefinedHTML: '&nbsp;'
    });
  4. Add the control to the map without forgetting that this operation is only available after you create the map:
    map.addControl(mousePosition);
  5. See the result in your browser and hover with your mouse ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required