谷歌地图折线infowindow

时间:2015-06-26 16:43:25

标签: google-maps polyline

继续this question,我尝试将相应的json对象名称添加到infowindow内容中,

 var infowindow = new google.maps.InfoWindow({
  content:i
  });

this fiddle中给出,但它仅显示最后一个对象的名称。我还尝试将return函数作为

google.maps.event.addListener(poly, 'click', (function(event, (poly, i)) {
    return function() {
  infowindow.open(map);
  infowindow.setPosition(event.latLng);
    }
  })(poly, i)); 

但没有用(fiddle)。我怎样才能实现它?

1 个答案:

答案 0 :(得分:1)

你说"但所有括号都正确关闭。"。这是不正确的(函数定义中有一组额外的括号):

google.maps.event.addListener(poly, 'click', (function(event, (poly, i)) {
    return function() {
      infowindow.open(map);
      infowindow.setPosition(event.latLng);
    }
  })(poly, i)); 

event参数属于返回的函数,您只需要对多边形(poly)和循环索引(i)进行闭包:

google.maps.event.addListener(poly, 'click', (function (poly, i) {
    return function (event) {
        infowindow.setContent(""+i);
        infowindow.setPosition(event.latLng);
        infowindow.open(map);
    };
})(poly, i));

updated fiddle