Google maps api close infowindow when clicking somewhere else

a 夏天 提交于 2019-12-25 04:26:54

问题


I have tried google it but can't find the one to apply to my code. i want my infowindow(s) to close everytime someone presses another place on the map or opens another infowindow somewhere else.

Can someone please help?

var myLatlng = new google.maps.LatLng(val.coords.split(',')[0].trim(),val.coords.split(',')[1].trim());

var infowindow = null;
        var contentString = '<div id="content">'+
  '<div id="siteNotice"> content'+
  '</div>';


        var infowindow1 = new google.maps.InfoWindow({
            content: contentString,
            maxWidth: 250
        });

        var marker1 = new google.maps.Marker({
            position: myLatlng,
            map: map,
            icon: image,
        });

        google.maps.event.addListener(marker1, 'click', function() {
            infowindow1.open(map,marker1);
        });


    });
});

回答1:


Checkout the code at JS Fiddle I added

google.maps.event.addListener(map, 'click', function(){
            infowindow1.close(map, marker1);
        });

Obviously to catch all the open items on the map, you'd have to use a small loop to catch 'em all within that function, but that's something I'll leave to you. I would add a variable to keep track of how many you've created, then loop "infowindow"+var_index to that max value, set 'em all to close. I'm still looking for a better way to grab all open infowindows, but in the meantime this technique will work.



来源:https://stackoverflow.com/questions/29134470/google-maps-api-close-infowindow-when-clicking-somewhere-else

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