2012-04-27 63 views
1

我有一个应用程序,我需要在infowindow气泡中的数据自动更新。我检查了一下,我看到了移动标记,更新标记颜色等的方法,但没有直接更新infowindow内容。我从内容自动更新的隐藏DOM元素中提取必要的值(例如building[i][7]),我需要infowindow的内容自动更新。谷歌地图Infowindow自动更新

这是我用来绘制infowindow气泡的代码。

infowindow=new google.maps.InfoWindow(); 
for (i=0;i<buildings.length;i++){ 
    marker=new google.maps.Marker({ 
     position:new google.maps.LatLng(buildings[i][4],buildings[i][5]), 
     map:map, 
     shadow:shadow, 
     icon:greenIcon, 
     title:buildings[i][0]+" \n"+buildings[i][1], 
     zIndex:buildings[i][6] 
    }); 
} 
google.maps.event.addListener(marker,'click',(function(marker,i){ 
    return function(){ 
     infowindow.setContent(
      '<h2>'+buildings[i][0]+'</h2>'+ 
      '<p>'+buildings[i][2]+'</p>'+ 
      '<p>'+buildings[i][7]+'</p>' 
     );infowindow.open(map,marker); 
    } 
})(marker,i)); 

任何帮助,非常感谢!

+0

是否有某种事件会触发,让您知道数据已更改?你的意思是说,只要隐藏的DOM元素的数据发生变化,你就想让InfoWindow内容发生变化? – 2012-04-27 20:14:22

+0

除了隐藏div的内容改变之外,没有任何事件会触发(是否有某种方式将其作为事件进行度量?)。 无论何时隐藏div的内容发生更改,您都希望InfoWindow内容发生更改,这是正确的。 – InterfaceGuy 2012-04-27 21:40:43

回答

1

没有在JavaScript中内置的方式通知时的DIV变化的内容,但我相信jQuery.bind()功能或更高版本(jQuery的V1 .7)jQuery.on()函数提供了一种方法来实现您正在尝试实现的内容。

+0

谢谢。我最近学到了'.delegate()','.live()'和'.on()',虽然我确信我可以让它们以某种方式工作,但我决定一起去不同的解决方案,它仅更新所选infowindow的值,并且仅在单击它时才更新。这解决了一些问题。 我很感激答案。干杯。 – InterfaceGuy 2012-05-01 15:25:55

+0

如何更新infowindow?可以在您的答案中更新您的解决方案?谢谢! – asdjkag 2014-09-08 07:21:06

0

上调用功能可按只要运行

infowindow.setContent(
     '<h2>'+buildings[i][0]+'</h2>'+ 
     '<p>'+buildings[i][2]+'</p>'+ 
     '<p>'+buildings[i][7]+'</p>' 
    );infowindow.open(map,marker); 
+0

这与我已有的有何不同? – InterfaceGuy 2012-04-27 18:36:21