[GIS] Changing longitude,latitude to latitude,longitude in OpenLayers 3

coordinatesjavascriptopenlayers

Openlayers is representing the coordenates as longitude,latitude.

Can I change the default format longitude,latitude to latitude,longitude?

By example, this will not work, because I have this array as [lat, lng] and LineString is taking it as [lng,lat]:

a = [
    [-21.929054, 64.127985],
    [-21.912918, 64.134726]
    ];
var lineString = new ol.geom.LineString(a);

Best Answer

The order of Lat/Lng is fairly embedded in the framework with no configuration entry. You can have a look at the code on github if you're interested: https://github.com/openlayers/ol3/tree/master/src/ol/geom

Unless you want to rewrite the parts of code that influence this, your only option is to format the data appropriately.

You might try something like this:

var a = [
    [-21.929054, 64.127985],
    [-21.912918, 64.134726]
];

var b = []
for (var i = 0; i < a.length; i++) {
  b.push([a[i][1], a[i][0]]);
}

var lineString = new ol.geom.LineString(b);