Search code examples
arrayscoordinatestransformopenlayerscoordinate-transformation

Openlayers: Incorrect transform coordinates?


I have got a problem with ol.proj.transform or ol.proj.fromLonLat with an array of coordinates. For test I try to use:

var my_array=new Array(); 
my_array[0]='13.494263,47.542546';
my_array[1]='13.675537,47.563928';
my_array[2]='13.763428,47.394399';
my_array[3]='13.562927,47.353266';
my_array[4]='13.689167,47.394167';
var my_array_length=my_array.length;
for (var i=0; i < my_array_length; i++)
  {  var col_my_array=my_array[i].split(",");
     var d=col_my_array[0];
     var s=col_my_array[1];
     var text_popup="Second " + col_my_array[i];
     var iconFeature = new ol.Feature
       (
          {  geometry: new ol.geom.Point(ol.proj.transform([d,s], 'EPSG:4326', 'EPSG:3857')), 
             some_text: text_popup  
          }
       );
     vectorSource2.addFeature(iconFeature);
  }

It is not work. The first number (d) is converted OK, but the second one (s) is not. If I use for example:

var test_coor = [13.689167, 47.394167]; 
var point_icon=new ol.proj.fromLonLat(test_coor);

or if I put the numbers

var point_icon=new ol.proj.transform([13.689167, 47.394167], 'EPSG:4326', 'EPSG:3857');

everything is OK too. But I do not know why I can not use an array of coordinates. I test it on Openlayers 3 or Openlayers 4 but the result is the same. On Openlayers 2 it is OK.

For example: if is used my_array[4] during loop, I get: 1523871.0998240844,-5252166.631004199 instead of: 1523871.0998240844,6006651.168265123


Solution

  • Don't transform strings! ol.proj.transform expects Array.<number>:

     var d = parseFloat(col_my_array[0]);
     var s = parseFloat(col_my_array[1]);