简体   繁体   English

谷歌地图infowindow关闭不工作

[英]google maps infowindow close not working

I have a google map contained in this fiddle 我有一个谷歌地图包含在这个小提琴

When you click on the pins they popup some information as expected and when you click on the x it closes the infowindow. 当您单击引脚时,它们会按预期弹出一些信息,当您单击x时,它会关闭信息窗口。

However when I click on the first pin, then click on additional pins (without clicking the x) the infowindows just keep poping up without removing the other ones. 但是,当我点击第一个引脚,然后单击其他引脚(不单击x)时,infowindows会一直弹出,而不会删除其他引脚。 how can I restructre my code to get this working? 我怎样才能重构我的代码以使其正常工作?

Html HTML

    <div id="map_canvas"></div>

Style 样式

    #map_canvas {
        float: left;
        height: 565px;
        width: 100%;
    }
    #content {
        min-width: 320px;
    }

JS JS

    var mylatlongs = [
                        {"rank":1,"name":"name 1","lat":"-25.901820984476252","lng":"135"},
                        {"rank":2,"name":"name 2","lat":"-25.901820984476252","lng":"135.05"},
                        {"rank":3,"name":"name 3","lat":"-25.901820984476252","lng":"135.025"}
                    ];
    var infowindow = null;
    jQuery(function() {
            var StartLatLng = new google.maps.LatLng(mylatlongs[0]['lat'], mylatlongs[0]['lng']);
            var mapOptions = {
                center: StartLatLng,
                streetViewControl: false,
                panControl: false,
                maxZoom:17,
                zoom : 13,
                zoomControl:true,
                zoomControlOptions: {
                    style:google.maps.ZoomControlStyle.SMALL
                }
            };

        var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

        jQuery.each( mylatlongs, function(i, m) {
            var StartLatLng = new google.maps.LatLng(-25.901820984476252, 134.00135040279997);

            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(m.lat, m.lng),
                bounds: true,
                id : 'mk_' + m.rank,
                letter : m.index,
                map: map,
                title: m.name
            });

            google.maps.event.addListener(marker, 'click', function() {
                if (infowindow) {
                    infowindow.close();
                }
                infowindow.open(map,marker);
                position: new google.maps.LatLng(m.lat, m.lng);
            });

            var contentString = '<div id="content">'+
              '<div id="siteNotice">'+
              '</div>'+
              '<h1 id="firstHeading" class="firstHeading">'+ m.name + '</h1>'+
              '<div id="bodyContent">'+ (m.rank) +
              '</div>'+
              '</div>';

            var infowindow = new google.maps.InfoWindow({
              content: contentString
            });

        });
    });

You have one info window for each marker, and one local variable for each one. 每个标记都有一个信息窗口,每个标记有一个局部变量。 When you try to close the previosuly opened info window, you are closing the one that belongs to the clicked marker instead. 当您尝试关闭previosuly打开的信息窗口时,您将关闭属于单击标记的那个窗口。

Create a single info window outside of the loop, and set the content of it in the click event so that it gets updated for the marker where you show it: 在循环外部创建一个信息窗口,并在click事件中设置它的内容,以便为显示它的标记更新它:

        var infowindow = new google.maps.InfoWindow({
            content: ''
        });

        jQuery.each( mylatlongs, function(i, m) {
            var StartLatLng = new google.maps.LatLng(-25.901820984476252, 134.00135040279997);

            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(m.lat, m.lng),
                bounds: true,
                id : 'mk_' + m.rank,
                letter : m.index,
                map: map,
                title: m.name
            });

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.close();
                infowindow.setContent(contentString);
                infowindow.open(map,marker);
            });

            var contentString = '<div id="content">'+
              '<div id="siteNotice">'+
              '</div>'+
              '<h1 id="firstHeading" class="firstHeading">'+ m.name + '</h1>'+
              '<div id="bodyContent">'+ (m.rank) +
              '</div>'+
              '</div>';

        });

Demo: http://jsfiddle.net/Guffa/GSX6G/3/ 演示: http//jsfiddle.net/Guffa/GSX6G/3/

        google.maps.event.addListener(marker, 'click', function() {
            if(marker.open){
                infowindow.close();
                marker.open = false;
            }
            else{
                infowindow.open(map,marker);
                marker.open = true;
            }
            google.maps.event.addListener(map, 'click', function() {
                infowindow.close();
                marker.open = false;
            });
        });

Define global objects 定义全局对象

var infowindow;

Updated this demo 更新了此演示

This is stated by google themselves. 这是谷歌自己说的。 By creating the InfoWindow outside the event handler, we ensure that there only exists one InfoWindow at a time. 通过在事件处理程序外部创建InfoWindow,我们确保一次只存在一个InfoWindow。 If we had put the code for creating the InfoWindow inside the event handler we would have created a new InfoWindow each time the marker was clicked. 如果我们在事件处理程序中放置了创建InfoWindow的代码,那么每次单击标记时我们都会创建一个新的InfoWindow。 Leaving us with several identical InfoWindows on top of each other. 让我们与几个相同的InfoWindows相互叠加。

This means if you declare infoWindow inside of the event handler like: 这意味着如果在事件处理程序中声明infoWindow,如:

 google.maps.event.addListener(marker, "click", function (e){
     var infoWindow = new google.maps.InfoWindow();
     infoWindow.setContent(props.content);
     infoWindow.open(map,marker);
 });

Then the infoWindows will keep popping up as you click on the markers 然后,当您单击标记时,infoWindows将不断弹出

However if they the infoWindow is global like: 但是,如果他们的infoWindow是全局的,如:

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

 google.maps.event.addListener(marker, "click", function (e){

     infoWindow.setContent(props.content);
     infoWindow.open(map,marker);
 });

OR 要么

var infoWindow;
google.maps.event.addListener(marker, "click", function (e){
     infoWindow = new google.maps.InfoWindow();
     infoWindow.setContent(props.content);
     infoWindow.open(map,marker);
 });

Then you will have only one InfoWindow Popping up 那么你将只有一个InfoWindow弹出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM