Have just one InfoWindow open in Google Maps API v3

后端 未结 10 922
盖世英雄少女心
盖世英雄少女心 2020-11-28 03:18

I need to have only one InfoWindow open on my Google Map. I need to close all other InfoWindows before I open a new one.

Can someone show me how to do this?

10条回答
  •  日久生厌
    2020-11-28 03:33

    You need to create just one InfoWindow object, keep a reference to it, and reuse if for all the markers. Quoting from the Google Maps API Docs:

    If you only want one info window to display at a time (as is the behavior on Google Maps), you need only create one info window, which you can reassign to different locations or markers upon map events (such as user clicks).

    Therefore, you may simply want to create the InfoWindow object just after you initialize your map, and then handle the click event handlers of your markers as follows. Let's say you have a marker called someMarker:

    google.maps.event.addListener(someMarker, 'click', function() {
       infowindow.setContent('Hello World');
       infowindow.open(map, this);
    });
    

    Then the InfoWindow should automatically close when you click on a new marker without having to call the close() method.

提交回复
热议问题