谷歌地图折线infowindow [英] google map polyline infowindow

查看:90
本文介绍了谷歌地图折线infowindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为这个问题的延续,我试图将各自的json对象名称添加到infowindow内容为,

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

这个小提琴,但它只显示最后一个物体的名字。我也尝试使用 return 函数作为

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

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

解决方案

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

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

事件参数属于返回的函数,并且只需要在多边形( poly )和循环索引( i )上关闭:



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

更新小提琴


As continuation to this question, i trying to add the respective json object name to infowindow content as,

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

Which is given in this fiddle but it displays only the name of last object. I also tried with return function as

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

but no use (fiddle). How can I achieve it?

解决方案

You said "But all brackets are correctly closed.". This is not correct (you have an extra set of parentheses in the function definition):

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

The event argument belongs to the returned function, and you only need closure on the polygon (poly) and the loop index (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

这篇关于谷歌地图折线infowindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆