Google API V3 multiple infowindows plus close on click

前端 未结 2 1707
情深已故
情深已故 2021-01-20 10:35

I figured out how to have multiple markers with info windows but they do not close when you click another marker, I believe it is because I am creating a new info window for

2条回答
  •  我在风中等你
    2021-01-20 11:17

    Create only one global InfoWindow object.

     //Global 
      var infowindow = new google.maps.InfoWindow();
    

    and then

        function add_marker(lat,lng,title,box_html) {
    
            var marker = new google.maps.Marker({
                  position: new google.maps.LatLng(lat,lng),
                  map: map,
                  title: title
            });
    
      google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent(box_html);
    
            infowindow.open(map,marker);
        });
        return marker;
      }
    

提交回复
热议问题