1

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);
3

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);
0
var ring = [[77.4700927734375, 12.906190219892437],[77.4707794189453, 12.987162237749473],[77.5188446044922, 12.989838549012532],[77.51060485839844, 12.961066692801282],[77.51747131347656, 12.953705916047227],[77.52708435058594, 12.94500653581774],[77.51815795898438, 12.936306851970144],[77.50030517578125, 12.92426063498786],[77.486572265625, 12.912883118595701],[77.4700927734375, 12.906190219892437]];
var polygon = new ol.geom.Polygon([ring]);
polygon.transform('EPSG:4326', 'EPSG:3857');     
var feature = new ol.Feature(polygon);

// Create vector source and the feature to it.
var vectorSource = new ol.source.Vector();
vectorSource.addFeature(feature);

// Create vector layer attached to the vector source.
var vectorLayer = new ol.layer.Vector({
  source: vectorSource
});

// Add the vector layer to the map.
map.addLayer(vectorLayer);  

This will work.

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.