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

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

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 Denunciar

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 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