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

185
Views
Show map markers on hover

I have a list of links to several locations. I would like to add map marker to my OSM map and remove it when I hover out, while adding new one I hover over etc. I've been been able to add marker to the map on hover but I'm not able to remove them.

HTML

<a href="/..." onmouseover="mkron(this)" onmouseout="mkroff()">...</a>

JS

function mkron(x) {
   // some code to get lat lon 
   L.marker([lat, lon]).addTo(map);
}

function mkroff() {
    markers.clearLayers();
}

What am I missing?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your problem is that you add the marker directly to the map instead of adding it to the LayerGroup / FeatureGroup markers.

I don't know the rest of your code but it should look like that:

var markers = L.featureGroup().addTo(map);

function mkron(x) {
   // some code to get lat lon 
   L.marker([lat, lon]).addTo(markers);
}

function mkroff() {
    markers.clearLayers();
}

The other variant would be to store the layer in a global variable and then remove it from the map:

var marker;

function mkron(x) {
   // some code to get lat lon 
   marker = L.marker([lat, lon]).addTo(map);
}

function mkroff() {
  if(marker){
    map.removeLayer(marker)
    marker = null;
  }
}
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!