Google maps: infowindow is not closing

前端 未结 2 1946
盖世英雄少女心
盖世英雄少女心 2020-12-07 05:48

My infowindows are not closing automatically when i click on the other marker.. here the code : http://pastebin.com/PvCt2z7W here is the code for markers with infowindows

2条回答
  •  無奈伤痛
    2020-12-07 06:38

    Instead of creating one infowindow for each marker, have one global infowindow variable. Then all you're doing in your marker click handler is updating the content for the marker each time. However your code will require this to be done with a closure, otherwise you're going to get each marker having the last marker's content.

    var infowindow = new google.maps.InfoWindow({
       maxWidth: 10
    });
    
    var arrMarkers = [];
    
    function addMarker(data) {
       var marker = new google.maps.Marker({
           position: new google.maps.LatLng(data.lattitude, data.longitude),
           map: map,
           title: data.address
       });
    
        arrMarkers.push(marker);
    
       var contentString = '
    '+ '
    '+ '
    '+ '

    '+data.name+'

    '+ '
    '+ '

    '+data.address+'

    '+ '

    '+ '

    Do You Want to change search location

    '+ ''+ '
    '+ '
    '; // add an event listener for this marker bindInfoWindow(marker, map, infowindow, contentString); } function bindInfoWindow(marker, map, infowindow, html) { google.maps.event.addListener(marker, 'click', function() { infowindow.setContent(html); infowindow.open(map, marker); }); } function clickLink(ID) { google.maps.event.trigger(arrMarkers[ID], 'click'); }

    Your HTML of sidebar links might look like:

    Link 1
    Link 2
    ...

提交回复
热议问题