2

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/

-1

Try this as your ol.control.MousePosition:

var mousePositionControl = new ol.control.MousePosition({
    coordinateFormat: function(coordinate) {
        lon = coordinate[0];
        lat = coordinate[1];
        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!

9
  • Okay, "coordinate[]" is the key to solution. And with modulo it's perfectly! Many thanks! – Suka Oct 5 '17 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 '17 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 '17 at 15:51
  • return ol.coordinate.format(coordinate, lat.toFixed(4) + ', ' + lon.toFixed(4)); – Suka Oct 5 '17 at 16:11
  • 1
    Lars! yet the wrapping in east from 180 to -180 is still broken. – Suka Oct 6 '17 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.