leaflet/JSONobject. Marker onclick show only last record

Posted by user2780898 on Stack Overflow See other posts from Stack Overflow or by user2780898
Published on 2013-10-30T18:05:57Z Indexed on 2013/11/03 9:54 UTC
Read the original article Hit count: 306

Filed under:
|
|
|

that my code

 <div id='map'></div>
 <div id="info"></div>

 [...] 

var markers1 = new L.MarkerClusterGroup( { showCoverageOnHover: true } );

 $.ajax({
    type: "GET",
    url: "db.php",
    success: function (result) {
        var JSONobject = JSON.parse(result);
        var jnCount = JSONobject.length;
        for (var i = 0; i < jnCount; i++) {
            var marker = new L.Marker(new L.LatLng(JSONobject[i]["lat"],JSONobject[i]["lng"]),{ icon: myIcon1 });

            var id = JSONobject[i]["id"];
            var list = "<dl>"

                    + "<dt><b>CITTA':</b> " + JSONobject[i]["citta_"] + "</dt>";

            marker.on('click', function()
 { {document.getElementById('info').innerHTML = list;} }); 


            markers1.addLayer(marker);
        }
        map.addLayer(markers1);

    }

});

Marker onclick shows only the last record! I think problem is in loop but I don't understand how fix it. Any idea?

Thanks

Nicola

© Stack Overflow or respective owner

Related posts about onclick

Related posts about leaflet