OpenLayers 3 Naming – How to Draw and Set Name in OpenLayers 3

javascriptopenlayers

I have following simple draw interaction:

draw = new ol.interaction.Draw({
    features: featureOverlay.getFeatures(),
    type: (type)
});

Now I would like to add attributes like a name and id to the feature. Is this possible? I couldn't find an awnser to this anywhere until now.

Does anyone know how to do this?

Best Answer

You just need to add an event listener on the draw variable

draw.on('drawend', function(e) {
  e.feature.setProperties({
    'id': 1234,
    'name': 'yourCustomName'
  })
  console.log(e.feature, e.feature.getProperties());
});

Be aware that there is a a function setId on e.feature but this "id" is a bit different from the one you set in the attributes (see the API docs)