Google Maps: Auto close open InfoWindows?

前端 未结 12 703
终归单人心
终归单人心 2020-12-02 05:30

On my site, I\'m using Google Maps API v3 to place house markers on the map.

The InfoWindows stay open unless you explicitly click the close icon. Meaning, you can h

12条回答
  •  长情又很酷
    2020-12-02 06:10

    Here is what I used if you are using many markers in a for loop (Django here). You can set an index on each marker and set that index every time you open a window. Closing the previously saved index:

    markers = Array();
    infoWindows = Array();
    var prev_infowindow =false;
    {% for obj in objects %}
    var contentString = 'your content'
    var infowindow = new google.maps.InfoWindow({
                content: contentString,
              });
    var marker = new google.maps.Marker({
                   position: {lat: {{ obj.lat }}, lng: {{ obj.lon }}},
                   map: map,
                   title: '{{ obj.name }}',
                   infoWindowIndex : {{ forloop.counter0 }}
              });
    google.maps.event.addListener(marker, 'click',
                function(event)
                {
               if( prev_infowindow ) {
                   infoWindows[prev_infowindow].close();
                    }
                    prev_infowindow = this.infoWindowIndex;
                    infoWindows[this.infoWindowIndex].open(map, this);
                }
            );
    
            infoWindows.push(infowindow);
            markers.push(marker);
    
          {% endfor %}
    

提交回复
热议问题