I have the features drawn manually in OpenLayers. Since I have managed to set the properties for my drawings, i.e. by this thread:
OpenLayers problem with adding features to custom drawings
I would like also to have the autoincrement ID.
I found a nice example here:
https://embed.plnkr.co/plunk/NgIpC3
which pretty much shows how to do it, although the GetID()
appears only once in this code.
My full code looks like this now:
var lineInteraction = new ol.interaction.Draw({
type: 'LineString',
source: vectorLayer.getSource()
});
lineInteraction.setActive(false);
lineInteraction.on('drawend', onDrawend);
lineInteraction.on('drawend', function(e) {
var title = prompt("Please provide the data", "untitled");
var value = prompt("Please provide the value", "undefinied");
e.feature.setProperties({
'Id': GetID(),
'Name': title,
'Value': value,
});
});
I am getting an error from my console that:
ReferenceError: GetID is not defined
I just need to increment my ID.
As per the example:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment
https://stackoverflow.com/questions/11026258/html-and-javascript-auto-increment-number
I tried to set the new variable and then assign it to my 'ID'
, although it throws ID 1 only
var lineInteraction = new ol.interaction.Draw({
type: 'LineString',
source: vectorLayer.getSource()
});
lineInteraction.setActive(false);
lineInteraction.on('drawend', onDrawend);
lineInteraction.on('drawend', function(e) {
var title = prompt("Please provide the data", "untitled");
var value = prompt("Please provide the value", "undefinied");
let x = 1
var id = x++
e.feature.setProperties({
'Id': id,
'Name': title,
'Value': value,
});
});
How can I make an autoincrement the ID of my drawing feature in OpenLayers?
utility.js
module in the mentioned Plunker nice example.let x = 1
as global var outside before'drawend'
processing function.ReferenceError: GetID is not defined
. Simply because you didn't include function definition in your code.