Categories
JavaScript Answers

How to auto close open InfoWindows with Google Maps?

Spread the love

Sometimes, we want to auto close open InfoWindows with Google Maps.

In this article, we’ll look at how to auto close open InfoWindows with Google Maps.

How to auto close open InfoWindows with Google Maps?

To auto close open InfoWindows with Google Maps, we call the InfoWindow’s close method.

For instance, we write

let prevInfowindow = false;
//...
base.attachInfo = (marker, i) => {
  const infoWindow = new google.maps.InfoWindow({
    content: "yourmarkerinfocontent",
  });

  google.maps.event.addListener(marker, "click", () => {
    if (prevInfowindow) {
      prevInfowindow.close();
    }

    prevInfowindow = infowindow;
    infoWindow.open(base.map, marker);
  });
};

to call prevInfowindow.close in the marker‘s click event listener to close the prevInfowindow InfoWindow.

We open the InfoWindow with infoWindow.open.

Conclusion

To auto close open InfoWindows with Google Maps, we call the InfoWindow’s close method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *