Can't setContent() an Array case with a Meter

15 views
Skip to first unread message

Kevin Tama

unread,
Mar 26, 2015, 5:52:45 AM3/26/15
to leafl...@googlegroups.com
Hi all,

I have a problem with the meter and display in the popup.
When I do this:

for (i=0;i<position.length;i++)
     {

        marker[i]= L.marker([position[i].Latitude,position[i].Longitude]).addTo(map);

        affichage[i] = (position[i].Latitude+' , '+position[i].Longitude);

        marker[i].on('mouseover', function(e) {
          popup = L.popup().setLatLng(e.latlng)
          .setContent(affichage[i].toString())
          .openOn(map);});

     }

Nothing appears but if I write the gross value of the case, like this, It works :


for (i=0;i<position.length;i++)
     {

        marker[i]= L.marker([position[i].Latitude,position[i].Longitude]).addTo(map);

        affichage[i] = (position[i].Latitude+' , '+position[i].Longitude);

        marker[i].on('mouseover', function(e) {
          popup = L.popup().setLatLng(e.latlng)
          .setContent(affichage[0].toString())
          .openOn(map);});

     }

Can you help me please ?:)

K.Tama

Alexandru Pufan

unread,
Mar 27, 2015, 2:19:12 AM3/27/15
to leafl...@googlegroups.com
First of all, you should create a layerGroup and store your markers in there.

var layerGroup = L.layerGroup().addTo(map);

The, create the markers, add them to the group and bind popups inside the for loop, but avoid creating functions inside.

for (var i=0; i < position.length; i++) {
  marker[i] =  L.marker([position[i][0],position[i][1]]).addTo(layerGroup);
  marker[i].bindPopup(position[i][0] + ', ' + position[i][1]);
}

Finally, make the popups to appear on mouseover, using eachLayer method of the layerGroup (outside the loop).

layerGroup.eachLayer(function(layer) {
  layer.on('mouseover', function(){
    this.openPopup();
  });
});

Here's an example:



Reply all
Reply to author
Forward
0 new messages