I want to add two markers to my leaflet map and assign popups to this markers.
My problem is, that I want to show the two popups simultaneously. On default leaflet allows only one popup to be shown.
I found in the leaflet documentation a solution for this: http://leafletjs.com/reference-1.0.0.html#popup So it is possible for me to show many popups on a map. This is my code for this:
<!DOCTYPE HTML>
<html>
<head>
<title>Eine OSM Karte mit Leaflet</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
</head>
<body>
<div style="height: 700px;" id="mapid"></div>
<script>
var mymap = L.map('mapid', {closePopupOnClick: false}).setView([50.27264, 7.26469], 7);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(mymap);
var popup1 = L.popup()
.setLatLng([49.27264, 6.26469])
.setContent('<p>Hallo Welt<br />Ich bin ein Popup.</p>')
.addTo(mymap);
var popup2 = L.popup({keepInView:true})
.setLatLng([49.27264, 5.26469])
.setContent('<p>Hallo Welt<br />Ich bin ein anderes Popup.</p>')
.addTo(mymap);
</script>
</body>
</html>
So I know that openon():
var popup1 = L.popup()
.setLatLng([49.27264, 6.26469])
.setContent('<p>Hallo Welt<br />Ich bin ein Popup.</p>')
.openOn(mymap);
var popup2 = L.popup({keepInView:true})
.setLatLng([49.27264, 5.26469])
.setContent('<p>Hallo Welt<br />Ich bin ein anderes Popup.</p>')
.openOn(mymap);
shows only one popup simultaneously.
But addTo() shows many popups simultaneously:
var popup1 = L.popup()
.setLatLng([49.27264, 6.26469])
.setContent('<p>Hallo Welt<br />Ich bin ein Popup.</p>')
.addTo(mymap);
var popup2 = L.popup({keepInView:true})
.setLatLng([49.27264, 5.26469])
.setContent('<p>Hallo Welt<br />Ich bin ein anderes Popup.</p>')
.addTo(mymap);
But I do not know how to use this popup in the separate layer for my marker. This is my code with the marker:
<!DOCTYPE HTML>
<html>
<head>
<title>Eine OSM Karte mit Leaflet</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
</head>
<body>
<div style="height: 700px;" id="mapid"></div>
<script>
var mymap = L.map('mapid', {closePopupOnClick: false}).setView([50.27264, 7.26469], 7);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(mymap);
var marker1 = L.marker([50.27264, 7.26469],
{
title:"Marker1",
alt:"Ich bin Marker 1"
}
).addTo(mymap);
var marker2 = L.marker([51.27264, 6.26469],
{
title:"Marker2",
alt:"Ich bin Marker 2"
}
).addTo(mymap);
marker1.bindPopup("<h1>Popup1</h1><p>text</p>");
marker2.bindPopup("<h1>Popup2</h1><p>text</p>");
</script>
</body>
</html>
Thanks in advance.