Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

298
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda