Google maps infoWindow - destroys content on close?
I am using google maps on my site. I have an infoWindow that I populate with content like this:
infowindow = new google.maps.InfoWindow({ content: $('infoWindowContent') });
google.maps.event.addListener(mapMarker, 'click', function() {
$('infoWindowContent').setStyle('display','block');
$('iw_companyName').innerHTML = "stuff";
$('iw_recommends').innerHTML = "stuff";
$('iw_activity').innerHTML = "stuff";
infowindow.open(googleMap,item);
});
$('infoWindowContent')
is an element of my page that I create in the html:
<div id="mapholder" class="box">
<div id="infoWindowContent" style="display:none;">
&l开发者_运维百科t;div class="center">
<h4><a id="iw_companyName"></a></h4>
<div>Activity: <a id="iw_activity"></a></div>
<div id="iw_recommends"></div>
</div>
</div>
<div id="map_canvas"></div>
</div>
So when the page is created, the content is invisible. When the user clicks a marker, the infoWindow appears there, and the content is populated with appropriate values. If the user clicks another marker, the infoWindow moves, and has its values changed.
The problem is when the user clicks the 'x' button on the top of the info window to close it. The close functionality works fine, but when the user clicks a marker again, I get the following error:
$("infoWindowContent") is null
[Break On This Error] $('infoWindowContent').setStyle('display','block');
So, $('infoWindowContent')
was destroyed when the infoWindow was closed.
How do I get around this? I don't really want to construct a new infoWindowContent in the javascript each time the user clicks a marker, as it is much harder to read/understand/change html generated in this fashion. Or is my way of approaching a solution here flawed?
Got the problem now. You are setting the div object as the content of infoWindow. So, the whole object is destroyed after closing infoWindow. Here is the fix, set only the HTML, not the object as content.
infowindow = new google.maps.InfoWindow({ content: $('infoWindowContent').get('html') });
精彩评论