Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet show all popups of all the makers and keep in the map

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?

like image 935
敬錞 潘 Avatar asked Dec 24 '22 08:12

敬錞 潘


1 Answers

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 &copy; <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:

  • each marker needs its own popup layer
  • the popup layers need to be configured with 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).
like image 128
Flopp Avatar answered Dec 29 '22 07:12

Flopp