0

I found a function that draws polygons, but I need it when drawing the second polygon, that it would always delete the previous polygon, since there must always be one polygon on my map

<!DOCTYPE html>
<html>
  <head>
    <title>Draw Features</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
  </head>
  <body>
<div id="map" class="map"></div>
<form class="form-inline">
  <label>Geometry type &nbsp;</label>
  <select id="type">
    <option value="Box">Box</option>
    <option value="None">None</option>
  </select>
</form>

which contains all the html and javascript code http://jsfiddle.net/p_tsagkis/c4o4put8/

Link to JS code

  • I tried your fiddle; it doesn't draw any polygons. Post a working sample of your code, something that demonstrates the problem. Also, single-step through your code with a debugger. If you still can't find the problem, come back here and discuss your debugging results, along with a code sample that runs and demonstrates the problem. – SaganRitual Mar 17 '18 at 19:49
  • Because the code is too large, I sent the link which contains all the html and javascript code http://jsfiddle.net/p_tsagkis/c4o4put8/ – Макс Азаренко Mar 18 '18 at 13:56
  • As I said before, single-step through your code with a debugger. If you still can't find the problem, come back here and discuss your debugging results. – SaganRitual Mar 18 '18 at 13:59
  • In my example, there are no errors, my question, if you read, is that I want to delete the rendered layer if I draw the next polygon, line, point – Макс Азаренко Mar 18 '18 at 14:11
  • Here's how StackOverflow works, if you read. You don't come here asking for help with something until you have tried to take care of it yourself. Then when you do come asking for help, you must show what you've tried, and you must discuss why your attempts don't work. As in, what you found out from the debugger. – SaganRitual Mar 18 '18 at 16:38
  • I tried quite a few different options, since I write here, which means it was a problem for me – Макс Азаренко Mar 18 '18 at 18:02
  • There is a reason that no one has answered your question. If you read, you will find the [guidelines](https://stackoverflow.com/help/how-to-ask) for posting a good question. – SaganRitual Mar 19 '18 at 07:03
  • You need to be less clever, I was given an answer, so keep your advice at yourself – Макс Азаренко Mar 20 '18 at 18:53
  • Stubborn, uncooperative, lazy people who flout the rules sometimes get lucky. I'm glad someone finally felt sorry for you. – SaganRitual Mar 20 '18 at 18:56
  • I'm glad that you can assert yourself with such primitive statements – Макс Азаренко Mar 20 '18 at 19:01
  • I'm sad that you are incapable of gratitude toward someone who came back numerous times to offer you helpful advice. As I say, you got lucky. Often, badly constructed, rule-breaking questions such as yours get deleted by the moderators. But we shouldn't argue. I will forget all of your rudeness and stubbornness, and I will pretend that you have finished our conversation properly, by **thanking** me for going so far out of my way to **help** you, despite your bad attitude. You are quite welcome. I'm glad to have helped you to learn how to behave in this forum. – SaganRitual Mar 20 '18 at 19:19

1 Answers1

1
  1. Use drawstart event on draw interaction.
  2. Use clear for source when drawstart.

You can access the features on the map through source of layer.
with your example on the fiddle, your features are contained in variable vector. and, as you see your code a while, there's drawend event on draw interaction which let you guess there's drawstart event as well.

// just like `drawend`
draw.on('drawstart', function(e) {

});

The thing you want is no feature displays when you start drawing so you can clear all feature on the source when drawstart

// map.getLayers().getArray()[1] is your vector layer
draw.on('drawstart', function(e) {
  map.getLayers().getArray()[1].getSource().clear();
});

http://jsfiddle.net/c4o4put8/25/

Chase Choi
  • 882
  • 11
  • 25