# ol.control.MousePosition EPSG:4326 over 180/-180 longitude (OL4)

i will use a OpenLayers example in EPSG:4326, but the value of longitude is not wrapping on 180/-180 longitude.

How can i fix it?
My first problem is, i can't pick the values of latitude and longitude in a value for a if calculation.

``````if (lon > 180) {
lon = (((lon / 180) - Math.floor(lon / 180)) * 180);
}
if (lon < -180) {
lon = (((((lon * -1) / 180) - Math.floor((lon * -1) / 180)) * (180 * -1) + 180));
}
``````

How do I extract the coordinates into a variable?
Or is there another solution?

The OpenLayers Example:
https://openlayers.org/en/latest/examples/mouse-position.html

The OL example in a JSFiddle:
https://jsfiddle.net/eqjjp4u8/

Try this as your `ol.control.MousePosition`:

``````var mousePositionControl = new ol.control.MousePosition({
coordinateFormat: function(coordinate) {
lon = coordinate;
lat = coordinate;
lon = lon % 360;
if (lon > 180) {
lon = lon - 360;
} else if (lon < -180) {
lon = 360 + lon;
}
modifiedCoordinate = [lon, lat]
return ol.coordinate.format(modifiedCoordinate, "{x}, {y}", 4);
},
projection: 'EPSG:4326',
className: 'custom-mouse-position',
target: document.getElementById('mouse-position'),
undefinedHTML: '&nbsp;'
});
``````

Check out your modified JSFiddle.

Update: Now using `modifiedCoordinate` to limit the number of digits!

• Okay, "coordinate[]" is the key to solution. And with modulo it's perfectly! Many thanks!
– Suka
Oct 5, 2017 at 10:27
• Please edit it with "return ol.coordinate.format(coordinate, '{y}, {x}', 4);". The digit-shortener "4" don't work in you JSFiddle.
– Suka
Oct 5, 2017 at 15:32
• oooops. Use ist with '{y}, {x}' destroy the wrapping function. OK, i have no solution on this time for the digit-shortener.
– Suka
Oct 5, 2017 at 15:51
• return ol.coordinate.format(coordinate, lat.toFixed(4) + ', ' + lon.toFixed(4));
– Suka
Oct 5, 2017 at 16:11
• Lars! yet the wrapping in east from 180 to -180 is still broken.
– Suka
Oct 6, 2017 at 13:11