rahram
rahram

Reputation: 590

Leaflet bindpopup inside loop

I am trying to show a list of points on a map and assign to each point its index as a popup event.

for(i = 0 ; i<data.length; i++){
var circle = L.circle([data[i].lat, data[i].lon], {
    radius: 5
}).bindPopup(toString(i)).addTo(mymap);
}

but in the web page when I click on a circle instead of showing a number it shows [object Undefined]

Thanks.

Upvotes: 2

Views: 516

Answers (1)

Rafael Kennedy
Rafael Kennedy

Reputation: 1004

Your code is correct except for the way you are casting a number to a string. The toString is a number method, so it needs to be called from the number object

Try the below code:

for(i = 0 ; i<data.length; i++){
var circle = L.circle([data[i].lat, data[i].lon], {
    radius: 5
}).bindPopup(String(i)).addTo(mymap);
}

or

for(i = 0 ; i<data.length; i++){
var circle = L.circle([data[i].lat, data[i].lon], {
    radius: 5
}).bindPopup(i.toString()).addTo(mymap);
}

Upvotes: 2

Related Questions