Google Maps: open InfoWindow on hover, close and click again

I have a marker page with InfoWindows that I opened in Click. I decided to rather open InfoWindows on MouseOver, which works.

But I believe that you need to move the mouse to the InfoWindow cross to close it, requiring a little for these lazy Internet visitors. So I added the Close event to Click of the Marker, which also works.

What I cannot understand is to be able to reopen InfoWindow on a marker click instead of being able to mouse to be able to re-mouse over a marker.

My code is:

google.maps.event.addListener(CalMarker, 'mouseover', function() { infowindow.setContent(contentStringCal); infowindow.open(map,CalMarker); }); google.maps.event.addListener(CalMarker, 'click', function() { infowindow.close(map,CalMarker); }); 

Can someone help me open the window by clicking on the marker?

Thanks in advance

PS: I can not say "Hello" at the beginning of the message, this is strange.

+6
source share
1 answer

Try the following:

 google.maps.event.addListener(CalMarker, 'mouseover', function() { //open the infowindow when it not open yet if(contentStringCal!=infowindow.getContent()) { infowindow.setContent(contentStringCal); infowindow.open(map,CalMarker); } }); google.maps.event.addListener(CalMarker, 'click', function() { //when the infowindow is open, close it an clear the contents if(contentStringCal==infowindow.getContent()) { infowindow.close(map,CalMarker); infowindow.setContent(''); } //otherwise trigger mouseover to open the infowindow else { google.maps.event.trigger(CalMarker, 'mouseover'); } }); //clear the contents of the infwindow on closeclick google.maps.event.addListener(infowindow, 'closeclick', function() { infowindow.setContent(''); }); 

Demo: http://jsfiddle.net/doktormolle/JXqLa/

+8
source

All Articles