Javascript Google Maps Events addlistener中的Access变量

Javascript Google Maps Events addlistener中的Access变量,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,切换功能: for(var i in coordenadas){ google.maps.event.addListener(marker[i], 'click', toggleBounce); } 在函数toggleBounce()中,如何访问变量I?此变量传入…addListener(marker[i]…最简单的解决方案是在创建标记时将其添加为标记的属性(只需小心命名,以免与google.maps.marker的现有属性冲突) function toggleBounce() {

切换功能:

for(var i in coordenadas){
  google.maps.event.addListener(marker[i], 'click', toggleBounce);
}

在函数toggleBounce()中,如何访问变量I?此变量传入…addListener(marker[i]…

最简单的解决方案是在创建标记时将其添加为标记的属性(只需小心命名,以免与
google.maps.marker的现有属性冲突)

function toggleBounce() {
      if (this.getAnimation() != null) {
        this.setAnimation(null);
        var i = "I WANT TO ACCESS OF VARIABLE i"
      } 
      else {
        this.setAnimation(google.maps.Animation.BOUNCE);
      }
    }
// or here (since you didn't provide your code that creates the markers)
for(var i in coordenadas){
  marker[i]._index = i;
  google.maps.event.addListener(marker[i], 'click', toggleBounce);
}

function toggleBounce() {
  if (this.getAnimation() != null) {
    this.setAnimation(null);
    var i = this._index;
  } 
  else {
    this.setAnimation(google.maps.Animation.BOUNCE);
  }
}