[GIS] String of coordinates into Openlayers-3 Linestring on Vector Layer

coordinate systemfeaturesopenlayersvector-layer

I getting a string of coordinates like so:

[[38.313072, -89.863845] ,[38.312675, -89.863586] ,[38.310405, -89.862091] ,[38.310405, -89.862091] ,[38.309913, -89.861976] ,[38.309768, -89.861976] ,[38.309768, -89.861976] ,[38.30965, -89.861991] ,[38.309558, -89.862045] ,[38.309513, -89.862121] ,[38.30949, -89.862236] ,[38.309494, -89.86235] ,[38.309562, -89.862594] ,[38.313106, -89.871429] ,[38.313892, -89.873191]]

I'm trying to convert this into an Openlayers-3 feature on a vector layer like so:

var str = [[38.313072, -89.863845] ,[38.312675, -89.863586] ......
var trnsStr = new ol.geom.LineString(lineStr);
var strPrj = ol.proj.transform(trnsStr, 'EPSG:4326', 'EPSG:3857');
vectorRoute = new ol.layer.Vector({
   source: new ol.source.Vector({
       features: new ol.Feature({
           geometry: new ol.geom.LineString(strPrj),
               name: 'Line'
       })
   })
});

Unfortunately, this isn't working for me. I get 'length' errors trying to transform the geom.

Ideas?

Best Answer

ol.proj.transform is used to transform single Coordinates, not geometries. Use the transform method of each ol.geom.Geometry instead:

var str = [[38.313072, -89.863845] ,[38.312675, -89.863586] ......
var trnsStr = new ol.geom.LineString(str);
trnsStr.transform('EPSG:4326', 'EPSG:3857');
vectorRoute = new ol.layer.Vector({
   source: new ol.source.Vector({
       features: new ol.Feature({
           geometry: trnsStr,
           name: 'Line'
       })
   })
});