[GIS] OpenLayers 3 doesn’t want to draw programmatically a polygon

javascriptopenlayers

I am using OpenLayers 3 and I am trying to draw a polygon using given coordinates, but it doesn't want to draw it.

var source = new ol.source.Vector();

            var ring = [
             [3139880.24789847, 5961935.332187176], [3179627.5026067616, 5972025.01992082],
             [3146606.706387566, 5927997.291628557], [3186353.9610958574, 5939615.719927904]];

            draw = new ol.interaction.Draw({
                source: source,
                type: 'Polygon',
                geometryFunction: ring,
            });

            draw.on('drawend', function (e) {
                var id = guid();
                e.feature.featureID = id;
                e.feature.setProperties({
                    'id': id,
                    'name': 'Polygon',
                    'description': 'Some values'
                })
                map.removeInteraction(draw);
            });
            map.addInteraction(draw);

Best Answer

Try as follow:

var source = new ol.source.Vector();
var ring = ...
var geometry = ol.geom.Polygon([ring]);
var feature = new ol.Feature({
   geometry: geometry,
});
source.addFeature(feature);
Related Question