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

428
Views
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 answers
Answer question

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 Report

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