当用户点击其他引脚时如何关闭以前的infowindow?

时间:2017-02-10 12:58:35

标签: javascript google-maps

我试图像这样$(function(){ // Always show last 3 comments: $( ".comment-box" ).each(function( index ) { $(this).children(".user-comment-box").slice(-3).show(); }); $(".see-more").click(function(e){ // click event for load more e.preventDefault(); var link = $(this); if (link.hasClass('showing-more')) { link.siblings(".user-comment-box.extended").hide(1, function() { link.text('Show more comments'); link.removeClass('showing-more'); }); link.siblings(".user-comment-box.extended").removeClass('extended'); } else { link.siblings(".user-comment-box:hidden").addClass('extended') link.siblings(".user-comment-box:hidden").show(1, function() { link.text('Show less comments'); link.addClass('showing-more'); }); } }); }); 关闭,但那不起作用。有什么建议吗?

marker.infowindow.close();

这是我的小提琴:

http://jsfiddle.net/pGBZD/860/

2 个答案:

答案 0 :(得分:2)

您应该将最后一个打开的窗口保持为变量(currentWindow),当您尝试打开其他窗口时,您可以关闭最后打开的窗口(请参阅下面的代码段)

 var currentWindow ;
     marker.addListener('click', (function(marker, i) {
        return function() {
        marker.infowindow.close();
          if (currentWindow) currentWindow.close();
          this.infowindow.setContent(locations[i][0], locations[i][6]);
          this.infowindow.open(map, marker);
          currentWindow =  this.infowindow;
        }
      })(marker, i));
    }



var locations = [
    [
        "New Mermaid",
        36.9079,
        -76.199,
        1,
        "Georgia Mason",
        "",
        "Norfolk Botanical Gardens, 6700 Azalea Garden Rd.",
        "coming soon"
    ],
    [
        "1950 Fish Dish",
        36.87224,
        -76.29518,
        2,
        "Terry Cox-Joseph",
        "Rowena's",
        "758 W. 22nd Street in front of Rowena's",
        "found"
    ],
    [
        "A Rising Community",
        36.95298,
        -76.25158,
        3,
        "Steven F. Morris",
        "Judy Boone Realty",
        "Norfolk City Library - Pretlow Branch, 9640 Granby St.",
        "found"
    ],
    [
        "A School Of Fish",
        36.88909,
        -76.26055,
        4,
        "Steven F. Morris",
        "Sandfiddler Pawn Shop",
        "5429 Tidewater Dr.",
        "found"
    ],
    [
        "Aubrica the Mermaid (nee: Aubry Alexis)",
        36.8618,
        -76.203,
        5,
        "Myke Irving/ Georgia Mason",
        "USAVE Auto Rental",
        "Virginia Auto Rental on Virginia Beach Blvd",
        "found"
    ]
]

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 12,
      // center: new google.maps.LatLng(-33.92, 151.25),
      center: new google.maps.LatLng(36.8857, -76.2599),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

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

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });
			marker.infowindow = new google.maps.InfoWindow({
                   noSupress: true,
                   content: ''
                 });
    var currentWindow ;
     marker.addListener('click', (function(marker, i) {
        return function() {
        marker.infowindow.close();
          if (currentWindow) currentWindow.close();
          this.infowindow.setContent(locations[i][0], locations[i][6]);
          this.infowindow.open(map, marker);
          currentWindow =  this.infowindow;
        }
      })(marker, i));
    }
&#13;
  
<div>
  <div id="map" style="width: 500px; height: 400px;"></div>

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试更改:google.maps.event.addListener(infowindow, 'closeclick' 至       google.maps.event.addListener(this.infowindow, 'closeclick'