0

I'm trying to add features manually to vector layer but with no luck. This is my code:

cords[0] = {
[19.1820888104, 49.6835693017],
[19.1819108957, 49.6850423191],
[19.1846518426, 49.6853635871],
[19.1864287796, 49.6845787109],
[19.1864490975, 49.6839272706],
[19.1853396287, 49.6834588915],
[19.1835425444, 49.6834155484],
[19.182316043, 49.6832563996],
[19.1820888104, 49.6835693017]}

var featurething = new ol.Feature({
    geometry: new ol.geom.LineString(cords[0])
    }); 
var layerV = new ol.layer.Vector({
        source: new ol.source.Vector({
            features: featurething
        }),
        visible: true
    }); 
map.addLayer(layerV);

When I check layerV object in console it looks okay (it has feature with proper geometry) but it's not displaying on map. Any idea what's wrong?

1 Answer 1

1

Here a correction (it seems your sample coordinates were not well formatted, made some changes)

You can try it copying and pasting in the browser debugger console at https://openlayers.org/en/v4.6.5/examples/simple.html

// It was not possible to use an object with multiple array,
// so some changed in input coordinates
var cords = [
  [19.1820888104, 49.6835693017],
  [19.1819108957, 49.6850423191],
  [19.1846518426, 49.6853635871],
  [19.1864287796, 49.6845787109],
  [19.1864490975, 49.6839272706],
  [19.1853396287, 49.6834588915],
  [19.1835425444, 49.6834155484],
  [19.182316043, 49.6832563996],
  [19.1820888104, 49.6835693017]
];

// Coordinates can be longitude, latitude (EPSG:4326) but you must
// convert them manually to map projection, spherical mercator (EPSG:3857)
// hence the transform function
var geometry = new ol.geom.LineString(cords).transform('EPSG:4326', 'EPSG:3857');

var featurething = new ol.Feature({
  geometry: geometry
});
// You were using an ol.Feature instead of an array of ol.Feature
var layerV = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [featurething]
  }),
  visible: true
});
map.addLayer(layerV);
// Add to zoom to prove it works
map.getView().fit(layerV.getSource().getExtent());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.