How do I add images (as a screenshot, for example) into individual popups on a Leaflet map hosted on GitHub? #209501
🏷️ Discussion TypeQuestion 💬 Feature/Topic AreaOther BodyHi everyone, my name is Warren and I’m new to the discussions forums on here but I’m not new to GitHub. I was wondering if anyone has any ideas, suggestions or any experiences in Leaflet maps, specifically CRS.simple, on how to add images (such as photos and screenshots) into individual popups of each markers on the map, the link is at the top as Map Link, for your convenience. If you have any way of doing this, as for the life of me this past week, I have no clue on how to add images in popups, I would greatly appreciate any advice or help on this matter. Happy coding everyone! Warren |
Replies: 2 comments 3 replies
|
You should be able to add images directly inside the Leaflet popup using normal HTML. For example, if you have something like: L.marker([100, 200])
.addTo(map)
.bindPopup("Some text");you can just put an L.marker([100, 200])
.addTo(map)
.bindPopup(`
<div>
<h3>Marker Title</h3>
<img src="images/example.jpg"
alt="Screenshot"
style="width:250px; max-width:100%; height:auto;">
<p>Some text here</p>
</div>
`);If you are hosting it through GitHub Pages, I would probably keep the images inside the same repo and use relative paths. Something like: Then for each marker you can just change the image path: L.marker([100, 200]).addTo(map).bindPopup(`
<img src="images/image1.jpg" style="width:250px; max-width:100%;">
`);
L.marker([150, 300]).addTo(map).bindPopup(`
<img src="images/image2.jpg" style="width:250px; max-width:100%;">
`);You can also control the popup size if the screenshots are large: L.marker([100, 200])
.addTo(map)
.bindPopup(
`<img src="images/image1.jpg" style="width:300px; max-width:100%;">`,
{ maxWidth: 320 }
);Basically, I hope this helps! |
|
const popupContent = ` ${loc.name} ID: ${loc.id} 📋 Copy Share Link And if I add a loc.img and then add the url for each image in the markers, this will provide the image, right? |
You should be able to add images directly inside the Leaflet popup using normal HTML.
CRS.Simpleshould not really change anything with how the popup itself works.For example, if you have something like:
you can just put an
<img>tag insidebindPopup():If you are hosting it through GitHub Pages, I would probably keep the ima…