How to zoom on marker click event in Mapbox Leaflet?

非 Y 不嫁゛ 提交于 2019-12-22 17:46:18

问题


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());
});

回答1:


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);
});



回答2:


Try

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


来源:https://stackoverflow.com/questions/29385063/how-to-zoom-on-marker-click-event-in-mapbox-leaflet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!