4

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?

3

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'
       })
   })
});
  • that returns trnsStr as : [object Object]. No errors show in debugger, but no vectorlayer is created either. – Dean Oct 18 '15 at 23:44
  • I fixed some issues with the code. Try again. If it fails, use the browsers developer tools to debug. – Alvin Lindstam Oct 19 '15 at 6:46
  • Yes, I already fixed those typos... same results. still makes them an object – Dean Oct 19 '15 at 10:38
  • Well, it is an object. And it should be. Could you describe the actual error? – Alvin Lindstam Oct 19 '15 at 10:41
  • No errors... but no layer. – Dean Oct 19 '15 at 21:17

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.