Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a click even to a Leaflet Marker

I am using Leaflet and I have several markers on the map. I am trying to trigger a click event so that when I click on one of the markers it will scroll to a div down the page

For example, the marker is created like this:

var marker = L.marker([ 51.5, -0.09 ]).addTo(map);

Can I add a click event to this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can bind event on your marker when you initialize it. When the click is fired you just redirect to the dom element you want :

var marker = L.marker([51.5, -0.09]).addTo(map).on('mouseover', () => {
    document.location.href = "#id_of_div";
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading