Google API V3 multiple infowindows plus close on click

前端 未结 2 1684
情深已故
情深已故 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 10:57

    Change your add_marker function to use a single global infowindow. One way:

    var infowindow = null;
    function add_marker(lat,lng,title,box_html) {
    
      infowindow = new google.maps.InfoWindow({
          content: 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;
    }
    

    Also described in this example in the "Demo Gallery"

    0 讨论(0)
  • 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;
      }
    
    0 讨论(0)
提交回复
热议问题