[GIS] Draw Square from points OpenLayers 3

openlayersopenstreetmap

I have one string with points:

var points =
  [-417494.99760966154,4921341.910474674,-416272.0051570987,4922111.058071794]

And I am trying to print a Box

var Square = new ol.geom.Polygon([points]);
var SquareFeature = new ol.Feature(Square);

source = new ol.source.Vector();
source.addFeature(SquareFeature);

But it not working.

How should I parse the points?

Best Answer

Your points declaration is faulty.

  1. Each point should be an array of two coords (x and y)
  2. A polygon is conformed with at least three pair of coords. You supply coordinates for a single line not for polygon.

Try this

var points = [[
[-417494.99760966154,4921341.910474674],
[-417494.99760966154,4922111.058071794],
[-416272.0051570987,4922111.058071794],
[-417494.99760966154,4921341.910474674]
]];
var Square = new ol.geom.Polygon(points);
var SquareFeature = new ol.Feature(Square);

var source = new ol.source.Vector({
  features: [SquareFeature]
});