2

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);
1
  • 1
    Geometry is stored [X,Y] by convention. Longitude is X. Your example will work, for generating a feature in the Indian Ocean east of Mauritus, but not for one near Reykjavik. – Vince May 22 '15 at 11:34
1

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);
2
  • Did you mean var lineString = new ol.geom.LineString(b);? – Vince May 22 '15 at 18:45
  • Absolutely.. edit made. – nagytech May 22 '15 at 20:48
0

Just as said in other answer, you can't change order in the framework itself.

An alternate syntax to revert your coordinates order could be:

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

var b = a.map(function(coord) {
  return [coord[1],coord[0]]
})

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

You can see that coordinates order choice per library and format varies at http://www.macwright.org/lonlat/

Your Answer

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