leafletjs标记带有选项 的bindpopup()

问题描述 投票:14回答:3

传单文档显示您可以向标记添加弹出窗口

marker.bindPopup("<b>Hello world!</b><br>I am a popup.").openPopup();

或创建一个独立的弹出窗口

var popup = L.popup()
    .setLatLng([51.5, -0.09])
    .setContent("I am a standalone popup.")
    .openOn(map);

有没有办法设置弹出选项并将其绑定到标记?我希望能够为弹出窗口设置自己的最大宽度,并在单击标记时打开/关闭它们。

popup leaflet marker
3个回答
10
投票

你确定你在读Leaflet reference documentation吗?它指定您可以通过创建弹出窗口并使用它来调用.bindPopup来绑定弹出窗口。例如,

var popup = L.popup()
    .setContent("I am a standalone popup.");

marker.bindPopup(popup).openPopup();

4
投票

对于maxWidth,您应该这样做:

var popup = L.popup({
    maxWidth:400
});
marker.bindPopup(popup).openPopup();

0
投票

您可以传递popup options的对象作为bindPopup的第二个参数,如下所示:

marker.bindPopup("<strong>Hello world!</strong><br />I am a popup.", {maxWidth: 500});

我已经在Leaflet 1.4中对此进行了测试,它似乎也可以在早期版本的bindPopup中使用。

© www.soinside.com 2019 - 2024. All rights reserved.