Google API V3 multiple infowindows plus close on click

陌路散爱 提交于 2019-12-31 02:09:12

问题


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 each marker, any help would be appreciated.

    <script type="text/javascript">


var map ;
  function initialize() {
    var latlng = new google.maps.LatLng(53.063165, -3.205390);
    var myOptions = {
      zoom: 6,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.SATELLITE,
      zoomControl: false,
      mapTypeControl: false,
      mapTypeControlOptions: {
          style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
          position: google.maps.ControlPosition.LEFT_CENTER
      },
panControl: false,
streetViewControl: false,
      streetViewControlOptions: {
          position: google.maps.ControlPosition.LEFT_CENTER
      }
};
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  var marker = add_marker(56.747923,-3.717155,"Ben Vrackie","<b>Ben Vrackie</b><br><br>Perthshire classic! some sweet hidden gems on this loop.<br> surrounded by amazing countryside, Lochs, hills this<br>has it all... Red Grade."); // pass in as Latitude, then Longitude
    marker.setMap(map);

   var marker = add_marker(57.556366,-5.409222,"Torridon Forest","<b>Torridon Forest</b><br><br>Technical and breathtaking.... <br>Black/red grade."); // pass in as Latitude, then Longitude
    marker.setMap(map)
}


  function add_marker(lat,lng,title,box_html) {

    var 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.open(map,marker);
});
return marker;
  }



 </script>

回答1:


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;
  }



回答2:


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"



来源:https://stackoverflow.com/questions/12567280/google-api-v3-multiple-infowindows-plus-close-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!