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

429
Visualizações
Leaflet js 'follow' a marker as it receives live updates from GPS devices and moves around the map. Also be able to stop tracking the marker

I have a leaflet map that is populated with markers when GPS devices POST their lat and long on random intervals.

The issue is I have to manually drag the map to follow the marker as it receives updates and moves around the map.

Hours later and much research effort:

I implemented a half solution using a button which snaps to the markers current position, but again I have to click the button every time it moves to keep the marker in sight.

onclick="mymap.fitBounds(gotomarker1.getBounds());"

Is there a way I could click the marker itself which will then keep the marker in the center of the map as it receives lat and log updates and moves around? *ideally not a button as there could be many markers and I wouldn't want a page full of buttons.

I have tried applying an on.click function that loops the gotomarker1 code above but I cant stop it once its started, this is an issue as there is more than one marker that I may want to follow or not follow (to clarify I only want to follow one marker at a time).

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

the way i have solved a similar problem is to store the marker that is being followed into a variable

onclick="selectedMarker = myMarker"

Whenever you receive location updates simply focus the map on the selectedMarker

// update all locations
// ...
// focus selected marker
mymap.fitBounds(selectedMarker.getBounds());
about 4 years ago · Juan Pablo Isaza Relatório

0

@Nejc Jaminiks logic in his answer helped me work out what I needed to do.

When a marker is created I have this code appended to the marker in real time:

.on('click', focus);

I then have a function to detect when the marker is clicked and loop through setting the view to the marker until the marker (or another marker) is clicked then it will stop following it.

var focusenabled = true;
interval = null;


function focus(e) {
  if (focusenabled) {
    var i = 0;
    interval = setInterval(function () {
      mymap.setView(e.target.getLatLng());
      console.log("focus active");
    }, 2000);
    focusenabled = false;
    document.getElementById("footer").style.backgroundColor='red';
    document.getElementById("footer").style.color='white';
    } else {
      clearInterval(interval);
      document.getElementById("footer").style.backgroundColor='#222';
      document.getElementById("footer").style.color='#222';
      focusenabled = true;
      }
  };

I use the code below to change the footer div to red and text to white when a marker is being followed:

document.getElementById("footer").style.backgroundColor='red';
document.getElementById("footer").style.color='white';

And then when a marker is not being followed the footer div returns to the background color which effectivley hides it :)

document.getElementById("footer").style.backgroundColor='#222';
document.getElementById("footer").style.color='#222';
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