0

Here is my code. But it's showing only last polygon which is iterated from array. (For binding polygon I am using array)

var polygon, i;
for (i = 0; i <= latlngs.length; i++)
{
    if (i < 2) {
        polygon = L.polygon(latlngs[i], { color: 'green' }).addTo(mymap);
    }
    else {
        polygon = L.polygon(latlngs[i], { color: 'red' }).addTo(mymap);
    }
    polygon.on('click', function (e) {
     mymap.fitBounds(polygon.getBounds());
    });       
}
1
  • It seems that you are reassigning the polygon variable at each iteration.
    – thibautg
    May 12, 2018 at 9:49

1 Answer 1

1
var polygon, i;
for (i = 0; i < latlngs.length; i++) {

    if (i < 2) {
        polygon = L.polygon(latlngs[i][0], { color: 'green', id : latlngs[i][1]}).addTo(mymap);
    }
    else {
        polygon = L.polygon(latlngs[i], { color: 'red', id: latlngs[i][1] }).addTo(mymap);
    }

    polygon.on('click', function () {
        //mymap.fitBounds(polygon.getBounds());
        mymap.fitBounds(this.getBounds());

        getChilds(this.options.id);
    });
2
  • if i<2, shouldn't it be point.
    – neogeomat
    May 13, 2018 at 18:01
  • No .. i have posted answer..
    – scorp_30
    May 13, 2018 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.