Reputation: 163
I am using OpenLayers 3 and I am trying to draw a polygon using given coordinates, but the polygon is not drawn. Here is what I've tried:
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);
Upvotes: 0
Views: 983
Reputation: 18034
Since you already have the coordinates, I don't think ol.interaction.Draw()
is suitable. Draw is for cases where the user is able to draw on the map.
Just use a vector layer and add it to the map like so:
var feature = new ol.Feature({
geometry: new ol.geom.Polygon(coordinates)
});
var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [ feature ]
})
});
map.add(vectorLayer);
Upvotes: 1