Google地图:google.maps.event.addListener

时间:2014-09-03 04:38:03

标签: javascript google-maps drawing

我有形状,我在地图中绘制:

    shapes; //array of shapes, i draw it from string that i take from db

    for (var i = shapes.length - 1; i >= 0; i--) {
        google.maps.event.addListener(shapes[i], 'click', function() {
            myFunc(shapes[i]); //function to edit shapes[i]
        });
    };

当我这样做时: 1.例如我的形状长度= 5; 2.我点击1的形状,但myFunc总是调用i = 5所以myFunc总是调用形状[5]

我已经尝试过了:Passing a pointer to objects method safely to google.maps.event.addListener

但仍然失败,有人可以给我建议吗?

1 个答案:

答案 0 :(得分:0)

形状; //形状数组,我从我从db

获取的字符串中绘制它
for (var i = shapes.length - 1; i >= 0; i--) {
 (function (i) {
    google.maps.event.addListener(shapes[i], 'click', function() {
        myFunc(shapes[i]); //function to edit shapes[i]
    });
 })(i);
};