Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
After drawing bounding rectangle on map, drawing another rectangle does not delete previous rectangle

I created a function that draws a bounding rectangle on the map. When I use create a new rectangle function that comes with leaflet the manually drawn rectangle is not cleared. How to clear the manually drawn rectangle when someone uses the create new rectangle?

function drawRectangle()

{

var north = document.getElementById("north").value;
 var west = document.getElementById("west").value;
var east = document.getElementById("east").value;
var south = document.getElementById("south").value;
 var lat_lon = [[north,east],[south,west]];

  var rectangle = L.rectangle(lat_lon, { draggable: true ,redraw:true});
  rectangle.addTo(map);
 map.fitBounds(rectangle.getBounds());
}

Manually Added Rectangle

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you declare rectangle outside of the drawRectangle function you can remove it from the map before you draw the next rectangle:

var rectangle;
function drawRectangle()
{
  if(rectangle)
    rectangle.remove(map)

  var north = document.getElementById("north").value;
  var west = document.getElementById("west").value;
  var east = document.getElementById("east").value;
  var south = document.getElementById("south").value;
  var lat_lon = [[north,east],[south,west]];

  rectangle = L.rectangle(lat_lon, { draggable: true ,redraw:true});
  rectangle.addTo(map);
  map.fitBounds(rectangle.getBounds());
}

Or if you want it to be removed when you click the rectangle button put that logic in the event handler:

map.on('editable:drawing:move', function (e) { 
  if(rectangle)
    rectangle.remove(map)
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!