Auto close infowindow when another is open (Google Map developer)

This one is for the developers fan, exactly for Google Map API developers, a simple but necessary option must be available on your Google Map UI, if your Map got multiple markers with infowindow for every marker, so your Map must automaically close the current infowindow when user click on another maker, on this fast post I'm going to learn you an easy method to do it.

The idea is to declare a new global variable "currentinfo" and listen to every marker, when an infowindow is open and user click on any one close the previous one which is stored on the declared variable "currentinfo", then affect the current infowindow to the "currentinfo", so the code is like bellow:


var currentinfo = null; // set the variable top null at first run.
var infowindow = new google.maps.InfoWindow({     content: "infowindow content here" }); google.maps.event.addListener(marker, 'click', function() { 
  if(currentinfo) { currentinfo.close();} 
  infowindow.open(map, marker);  
 
currentinfo = infowindow; }); 
The Red lines are the magic lines, use them on your application if it differs from the bellow algorithm.



If you like this article, please share it on facebook, tweet it and +1 it to spread it :) !
Auto close infowindow when another is open (Google Map developer) Auto close infowindow when another is open (Google Map developer) Reviewed by Mhr on 22:27 Rating: 5

No comments

No Backlinks please, Comments are under moderation !