Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zoom on marker click event in Mapbox Leaflet?

I want to zoom on a marker when it is clicked. I am using Mapbox and leaflet.

I tried:

marker.on('click', function(e){
    map.setView([e.lat, e.lng], 12);
});

But it gives me some kind of error:

TypeError: t is null

I even tried:

marker.on('click', function(e){
    map.fitBounds(marker.getBounds());
});
like image 213
Rohan Avatar asked Dec 05 '22 22:12

Rohan


2 Answers

To get the latitude and longitude of the event, you must use e.latlng: latlng reference. Use this:

marker.on('click', function(e){
    map.setView(e.latlng, 13);
});
like image 159
Alexandru Pufan Avatar answered Dec 08 '22 12:12

Alexandru Pufan


Try

marker.on('click', function(e){
    map.setView([e.latlng.lat, e.latlng.lng], 12);
});
like image 38
asir6 Avatar answered Dec 08 '22 11:12

asir6