简体   繁体   English

使用Leaflet在弹出内容中添加图像

[英]Adding an image inside popup content with Leaflet

after searching deeper in many questions and in many examples I couldn't get it working. 在深入研究了许多问题和许多示例之后,我无法使它正常工作。 I've trying anything written in any example. 我正在尝试任何示例中编写的任何内容。

I want to add an image inside a popup marker without using the amazing Leaflet.photo plugin this time. 我想这次在弹出标记内添加图像,而不使用惊人的Leaflet.photo插件。

My problem is that the image goes away from the popup boundaries the first time I open it out. 我的问题是,当我第一次打开图像时,图像会偏离弹出窗口的边界。 After closing it and open it again, the image fits perfectly within the boundaries. 将其关闭并再次打开后,图像将完全适合边界。

The same as it happens in this example example 因为它在这个例子中发生同样的例子

I also tryed to hack the CSS class in following way instead of write custom options as in the example source code. 我还尝试通过以下方式修改CSS类,而不是像示例源代码中那样编写自定义选项。

.leaflet-popup-content img { max-width:234px !important; height:auto; width:auto !important; } 

Nothing changed though, the result is the same as in the example shown. 虽然没有任何变化,但结果与所示示例相同。 Any suggestion? 有什么建议吗?

Thanks a lot 非常感谢

What may happen is that the image takes some time to download, hence Leaflet does not know the correct size to give to the popup container. 可能发生的情况是图像需要花费一些时间来下载,因此Leaflet不知道要赋予弹出式容器的正确大小。

Then on second opening, the image is fetched from browser cache, and Leaflet can properly compute the popup size. 然后在第二次打开时,从浏览器缓存中获取图像,Leaflet可以正确计算弹出窗口的大小。

You can try a delayed myPopup._updateLayout() call to have Leaflet re-evaluate the popup container size once your image is downloaded. 您可以尝试延迟myPopup._updateLayout()调用,以使Leaflet在下载图像后重新评估弹出容器的大小。 Eg attach an onload listener to your image element, with a callback that calls the _updateLayout() method on the popup. 例如,将一个onload侦听器附加到您的图像元素,并带有一个在弹出窗口中调用_updateLayout()方法的回调。

See also leafletjs adding scrollable pop up? 另请参见leafletjs添加可滚动弹出窗口?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM