I'm trying to include pictures in my leaflet popups, but the popup-size doesn't adjust to the picture size. I found a solution to this on github:
https://github.com/Leaflet/Leaflet/issues/724
While that solution fixes the size of the popup, the result is shifted and the popup doesn't show up centered over the icon/marker... Any way to change that?
.leaflet-popup-content {
width:auto !important;
}
Also the other proposed solution (setting the maxWidth on the popup) doesn't help in my case. The popup width keeps on being the default width of 300px...
function pop_Fotos(feature, layer) {
var popupContent = '<img style="max-height:500px;max-width:500px;" src="...");
layer.bindPopup(popupContent, {maxWidth: 500, closeOnClick: true});
}
None of the before added answers worked for me, but I came upon a quite simple solution (the link of which I can no longer find). Just wrap your image in a div:
And add a simple css rule that corresponds to the image width:
Now the popup should load correctly and it solves the need for updates or closing/opening again.
Simply specifying
maxWidth: "auto"
option on the popup seems enough…Demo: http://jsfiddle.net/3v7hd2vx/29/
EDIT
Unfortunately, if the image is not loaded yet in browser cache, the popup will open right away with default size, and adjust its size but not its position once the image is fully loaded and displayed. As a result, the popup is shifted and its arrow is misplaced compared to the marker it is bound to.
A simple workaround is to listen to the image
"load"
event and to re-open the popup at that moment:Updated demo: http://jsfiddle.net/3v7hd2vx/32/
EDIT 2
Here is a more general solution: capture the
<IMG>
's"load"
event on all Leaflet popups and force them toupdate()
every time.Demo: https://jsfiddle.net/3v7hd2vx/277/
Reference: Leaflet issue #5484 (comment)
I had a similar issue with a Leaflet based map in Drupal where I needed to update the popup after loading the images and I managed to solve this based on @frankieandshadow 's answer.
Here is the code I came up with:
There is an update function for popups (which the doc says is for just this purpose): http://leafletjs.com/reference.html#popup-update .
Also, an event for when a pop up is opened: http://leafletjs.com/reference.html#path-popupopen .
And the event passed to the event handler identifies the popup: http://leafletjs.com/reference.html#popup-event-popup
My pop up content was a bit more varied than just a simple image, but the width was, of course, still affected by any the load time for images in the content (and was ok once the image was in the browser cache).
So when I hit this problem, in summary what I did using jQuery: