Google maps 关于清除/删除谷歌地图信息框(自定义信息窗口)的问题

Google maps 关于清除/删除谷歌地图信息框(自定义信息窗口)的问题,google-maps,google-maps-api-3,infobox,Google Maps,Google Maps Api 3,Infobox,请您看一下并告诉我为什么我不能: 1-打开新信息框时关闭任何打开的信息框 2-当用户单击“清除”按钮时,是否清除/删除打开的信息框 这是我的密码 $(document).ready(function() { var markers = []; // makers array var infoBox = null; var myOptions = { // map settings zoom: 15, center: new google.map

请您看一下并告诉我为什么我不能:

1-打开新信息框时关闭任何打开的信息框

2-当用户单击“清除”按钮时,是否清除/删除打开的信息框

这是我的密码

$(document).ready(function() {
    var markers = []; // makers array
  var infoBox = null;
    var myOptions = { // map settings
        zoom: 15,
        center: new google.maps.LatLng(38.721759, -9.151692),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        sensor: 'true'
    }
    var map = new google.maps.Map(document.getElementById("canvas-map"), myOptions);

    var data = [ // map data
    {
        id: 1,
        content: '<div class="img"></div><div class="txt"><h1>THIS TITLE</h1><span>Sed posuere consectetur est at lobortis. Donec sed odio dui. Donec sed odio dui. Aenean eu leo quam.</span></div><div class="fot"></div>',
        position: {
            lat: 38.721759,
            lng: -9.151692
        }
    },
    {
        id: 2,
        content: '<div class="img"></div><div class="txt"><h1>THIS TITLE</h1><span>This is a test.</span></div><div class="fot"></div>',
        position: {
            lat: 38.720805,
            lng: -9.146585
        }
    }
    ]

    for (var i = 0; i < data.length; i++) {
        var current = data[i];

        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(current.position.lat, current.position.lng),
            map: map,
            content: current.content
        });

        markers.push(marker);

        google.maps.event.addListener(markers[i], "click", function(e) {
              infoBox = new InfoBox({
                latlng: this.getPosition(),
                map: map,
                content: this.content
            });
        });
    }


   function clearMap(){
    for(i=0; i<markers.length; i++){
            markers[i].setMap(null);
        }
            markers = [];   
         infoBox.close();
    }

   $("#clear").on("click",function(){
       clearMap();
    });   
});
我得到的
未捕获类型错误:未定义不是
.close()
的函数。我还尝试了
.remove()
从地图中删除信息框,但放大或缩小时,框会再次出现在地图上

为了关闭现有的信息框,我还尝试了

 if (infoBox) {
            infoBox.close();
      }
在接下来的cod中,但它也没有完成任务

google.maps.event.addListener(markers[i], "click", function(e) {
 if (infoBox) {
        infoBox.close();
  }
          infoBox = new InfoBox({
            latlng: this.getPosition(),
            map: map,
            content: this.content
        });
    });
}
谢谢

  • 您的信息框没有.close()方法,请改用.setMap(null)
  • 如果只希望有一个信息框,请仅创建一个(第一次打开时,因为它需要一个板条),并将其移动到相应的标记处。需要编写“移动”信息框的方法
  • 单击“移动”信息框的侦听器:

    google.maps.event.addListener(markers[i], "click", function (e) {
        if (!infoBox) {
            infoBox = new InfoBox({
                latlng: this.getPosition(),
                map: map,
                content: this.content
            });
        } else {
            infoBox.setOptions({
                map: map,
                content: this.content
            });
            infoBox.setPosition(this.getPosition());
        }
    });
    
    /* move the InfoBox
     */
    InfoBox.prototype.setPosition = function(latlng) {
        this.latlng_ = latlng;
    };
    

  • 您的信息框没有.close()方法,请改用.setMap(null)
  • 如果只希望有一个信息框,请仅创建一个(第一次打开时,因为它需要一个板条),并将其移动到相应的标记处。需要编写“移动”信息框的方法
  • 单击“移动”信息框的侦听器:

    google.maps.event.addListener(markers[i], "click", function (e) {
        if (!infoBox) {
            infoBox = new InfoBox({
                latlng: this.getPosition(),
                map: map,
                content: this.content
            });
        } else {
            infoBox.setOptions({
                map: map,
                content: this.content
            });
            infoBox.setPosition(this.getPosition());
        }
    });
    
    /* move the InfoBox
     */
    InfoBox.prototype.setPosition = function(latlng) {
        this.latlng_ = latlng;
    };
    

  • 您的信息框没有.close()方法,请改用.setMap(null)
  • 如果只希望有一个信息框,请仅创建一个(第一次打开时,因为它需要一个板条),并将其移动到相应的标记处。需要编写“移动”信息框的方法
  • 单击“移动”信息框的侦听器:

    google.maps.event.addListener(markers[i], "click", function (e) {
        if (!infoBox) {
            infoBox = new InfoBox({
                latlng: this.getPosition(),
                map: map,
                content: this.content
            });
        } else {
            infoBox.setOptions({
                map: map,
                content: this.content
            });
            infoBox.setPosition(this.getPosition());
        }
    });
    
    /* move the InfoBox
     */
    InfoBox.prototype.setPosition = function(latlng) {
        this.latlng_ = latlng;
    };
    

  • 您的信息框没有.close()方法,请改用.setMap(null)
  • 如果只希望有一个信息框,请仅创建一个(第一次打开时,因为它需要一个板条),并将其移动到相应的标记处。需要编写“移动”信息框的方法
  • 单击“移动”信息框的侦听器:

    google.maps.event.addListener(markers[i], "click", function (e) {
        if (!infoBox) {
            infoBox = new InfoBox({
                latlng: this.getPosition(),
                map: map,
                content: this.content
            });
        } else {
            infoBox.setOptions({
                map: map,
                content: this.content
            });
            infoBox.setPosition(this.getPosition());
        }
    });
    
    /* move the InfoBox
     */
    InfoBox.prototype.setPosition = function(latlng) {
        this.latlng_ = latlng;
    };
    

    带有链接中的代码的信息框。链接中的代码包含信息框。链接中的代码包含信息框。链接中的代码包含信息框。