1

I have the polygon coordinates defined as a variable. This draws nothing when I use the variable, when the same coordinates are used as such in the polygonFeatureT definition, the polygon is drawn correctly.

With point coordinates as variable this works, provided that the quotes are not used.

What am I doing wrong here?

The code is below:

var polyCoords = '[3143090.603086447, 9928281.393790578], [3283734.7351311715, 9928892.890016861], [3181003.3691158947, 9849398.380600277], [3143090.603086447, 9928281.393790578]';

// this Point var works only when no quotes are used in definiton!
var pointCoords = [3229617.319105267, 9916160.39109719];

var polygonFeatureT = new ol.Feature(new ol.geom.Polygon([[polyCoords]]));
var pointFeatureK = new ol.Feature(new ol.geom.Point(pointCoords));

var map = new ol.Map({

        target: 'map',
        layers: [
          new ol.layer.Tile({
              source: new ol.source.OSM()
          }),
               new ol.layer.Vector({
                   source: new ol.source.Vector({
                       features: [polygonFeatureT, pointFeatureK]
                   }),
...
2

If your data is a string you will need add any extra [] then convert it into an array of coordinates

var polyCoords = '[3143090.603086447, 9928281.393790578], [3283734.7351311715, 9928892.890016861], [3181003.3691158947, 9849398.380600277], [3143090.603086447, 9928281.393790578]';
var pointCoords = '[3229617.319105267, 9916160.39109719]';

var polygonFeatureT = new ol.Feature(new ol.geom.Polygon(JSON.parse('[[' + polyCoords + ']]')));
var pointFeatureK = new ol.Feature(new ol.geom.Point(JSON.parse(pointCoords)));
  • Hi, thank you very much - this worked and you saved my day! – Arto Vitikka May 8 at 10:16
  • @ArtoVitikka It would be appropriate then that you mark this answer as accepted. – TomazicM May 8 at 12:05
  • Thanks for noticing me on this - first time around! – Arto Vitikka May 9 at 6:01

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.