0

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?

5
  • Have a look at utility.js module in the mentioned Plunker nice example.
    – TomazicM
    Sep 27, 2021 at 14:02
  • As for what you have tried, put let x = 1 as global var outside before 'drawend' processing function.
    – TomazicM
    Sep 27, 2021 at 14:06
  • I saw already the utility.js, but the function applies to the date only as I see.
    – Geographos
    Sep 27, 2021 at 14:29
  • 1
    Question was why do you get error ReferenceError: GetID is not defined. Simply because you didn't include function definition in your code.
    – TomazicM
    Sep 27, 2021 at 14:37
  • Yes, I agree, thank you for showing me this function. I have forgotten to check other .js files. Will remember it in the future.
    – Geographos
    Sep 27, 2021 at 14:48

1 Answer 1

0
   var idn ;
      function GetID(idn ){
    var tt = idn;
       tt++;
     return tt;
    }

 var lineInteraction = new ol.interaction.Draw({
 type: 'LineString',
 source: vectorLayer.getSource()
 });
 var idt=0;
 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(idt++),
  'Name': title,
  'Value': value,
   });
 });
1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 28, 2022 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.