I refer to Quick Start Guide - Leaflet - a JavaScript library for interactive maps to implement show marker on the map.
I want to show all popups of all the markers,and if I click on the map,it still keep the popups.
The bottleNeck is
1.How to change the code to show multiple popups of the markers
2.How to keep the popups if I click on the map
Because I google this,I can't find the solution. Anybody can help me?
Here's a modified version of Leaflet's quickstart tutorial. It adds three markers with their own, individual popups and keeps the open:
var mymap = L.map('mapid').setView([51.505, -0.09], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'mapbox.streets'
}).addTo(mymap);
var markers = [
{pos: [51.51, -0.10], popup: "This is the popup for marker #1"},
{pos: [51.50, -0.09], popup: "This is the popup for marker #2"},
{pos: [51.49, -0.08], popup: "This is the popup for marker #3"}];
markers.forEach(function (obj) {
var m = L.marker(obj.pos).addTo(mymap),
p = new L.Popup({ autoClose: false, closeOnClick: false })
.setContent(obj.popup)
.setLatLng(obj.pos);
m.bindPopup(p);
});
The key points are:
autoClose: false
(=> the popup is not closed when another popup is opened) and closeOnClick: false
(=> the popup is not closed when the map is clicked).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With