Google Maps Infowindow Auto Updating

[亡魂溺海] 提交于 2019-12-11 04:24:13

问题


I have an app in which I need the data in the infowindow bubbles to update automatically. I've checked around and I see methods for moving markers, updating marker colors, and the like, but nothing with directly updating infowindow content. I am pulling the value necessary (e.g. building[i][7] ) from a hidden DOM element whose content automatically updates, and I need the infowindow's content to automatically update alongside it.

Here is the code I am using to draw the infowindow bubbles.

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));

Any help is greatly appreciated!


回答1:


There isn't a built-in way in JavaScript to be notified when the content of a DIV changes, but I believe the jQuery.bind() function or the newer (jQuery v1.7) jQuery.on() function provide a way to implement what you are trying to achieve.




回答2:


on funciton call just run

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


来源:https://stackoverflow.com/questions/10355852/google-maps-infowindow-auto-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!