javascript 如何放大 Mapbox Leaflet 中的标记点击事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29385063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:27:33  来源:igfitidea点击:

How to zoom on marker click event in Mapbox Leaflet?

javascriptjqueryleafletmapbox

提问by Rohan

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

我想在单击时放大标记。我正在使用 Mapbox 和传单。

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

类型错误:t 为空

I even tried:

我什至试过:

marker.on('click', function(e){
    map.fitBounds(marker.getBounds());
});

回答by Alexandru Pufan

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

要获取事件的经纬度,必须使用 e.latlng: latlng reference。用这个:

marker.on('click', function(e){
    map.setView(e.latlng, 13);
});

回答by asir6

Try

尝试

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