Search code examples
leafletpopuppolyline

Leafletjs polyline does not show popup


I have several dots distributed along a map and several polylines, connecting those dots. So far, so good.

Now I wanted to add a popup to each of those polylines, so they would show up when ever I click on them. It's not happening. This is what I got:

for (var key in JLinks) {

    var lat1 = JLinks[key]["lat1"];
    var lat2 = JLinks[key]["lat2"];
    var lon1 = JLinks[key]["lon1"];
    var lon2 = JLinks[key]["lon2"];
    var d    = JLinks[key]["d"];

    var line = L.polyline( [ [lat1,lon1], [lat2,lon2] ] );
    line.bindPopup(d);
    line.addTo(map);
}

The lines are placed correctly in the map, but the popups do not appear.


Solution

  • Well, the solution was rather easy (and unexpected to me ...). I had to concatenate the d parameter with some string, and now I have the popup being shown:

    var line = L.polyline( [ [lat1,lon1], [lat2,lon2] ] );
    line.bindPopup(d + "km");
    line.addTo(map);