Follow these steps to get started with the mouse position behavior:
Reuse the usual HTML for the chapter by copying it in a new file.
Now, edit the file to be sure that you create an HTML element with an ID
myposition
such as<div id="myposition"></div>
.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: ' ' });
Add the control to the map without forgetting that this operation is only available after you create the map:
map.addControl(mousePosition);
See the result in your browser and hover with your mouse the map.
Go back in your HTML code and add the following code in your CSS file,
assets/css/samples.css
:#myposition > .ol-mouse-position { position: relative; margin-left: 20px; font-size: 30px; }
Reload your page and you...