Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

289
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda