1

我正在使用 Google Maps Utility Library V3 中的 Infobox,在不跟踪所有信息框或添加事件侦听的情况下一次性删除所有信息框时遇到了一些麻烦。

我目前正在使用以下代码。

function initMarkers(map, markerData) {


    for(var i=0; i<markerData.length; i++) {

    var iconimage = "markers/" + trackall + ".png";
    marker = new google.maps.Marker({
            map: map,
            draggable: false,
            position: markerData[i].latLng,
            visible: true,
            icon: iconimage
        }),
        boxText = document.createElement("div"),
        //these are the options for all infoboxes
        infoboxOptions = {

         content: boxText
        ,disableAutoPan: true
        ,maxWidth: 0
        ,pixelOffset: new google.maps.Size(20, -75)
        ,zIndex: null
        ,boxStyle: {
          background: "url('') no-repeat"
          ,opacity: 0.75
          ,width: "140px"
         }
        ,closeBoxMargin: "10px 2px 2px 2px"
        ,closeBoxURL: ""
        ,infoBoxClearance: new google.maps.Size(1, 1)
        ,isHidden: false
        ,pane: "floatPane"
        ,enableEventPropagation: false
    };

        newMarkers.push(marker);
        //define the text and style for all infoboxes
        boxText.style.cssText = "border: 1px solid black; margin-top: 5px; background: #E0E7EF; padding: 5px;";
        boxText.innerHTML = markerData[i].address + "<br>" + markerData[i].state;
        //Define the infobox

        newMarkers[i].infobox = new InfoBox(infoboxOptions);
        //Open box when page is loaded

        newMarkers[i].infobox.open(map, marker);

        //Add event listen, so infobox for marker is opened when user clicks on it.  Notice the return inside the anonymous function - this creates
        //a closure, thereby saving the state of the loop variable i for the new marker.  If we did not return the value from the inner function,
        //the variable i in the anonymous function would always refer to the last i used, i.e., the last infobox. This pattern (or something that
        //serves the same purpose) is often needed when setting function callbacks inside a for-loop.
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                //newMarkers[i].infobox.open(map, this);
                //map.panTo(markerData[i].latLng);
                //newMarkers[i].infobox.close();
            /}
        })(marker, i));
    }

    return newMarkers;

}

//here the call to initMarkers() is made with the necessary data for each marker.  All markers are then returned as an array into the markers variable
markers = initMarkers(map, [
    { latLng: pointall}

]);

在上面的示例中,我使用了包含不断变化的标记信息的变量 pointall。该站点是一个航班跟踪站点,因此它不断跟踪不同位置的任意数量的飞机。每次有更新,例如。要绘制新标记,我使用以下函数首先删除旧标记。

function clearOverlays() {
  if (newMarkers) {
    for (var i = 0; i < newMarkers.length; i++ ) {
    newMarkers[i].setMap(null);
    }
  }
}

以上删除了所有标记,但如果我添加

newMarkers[i].infobox.close();

它删除第一个标记但停止执行代码。

有没有一种简单的方法可以说.. 关闭所有打开的信息框。我不需要知道哪个是哪个,因为它们都需要关闭。目前,信息框使用新标记打开,但旧信息框仍然存在。

注意:我不熟悉 javascript,所以对我来说很容易;)

任何帮助,将不胜感激。

谢谢

编辑:我在这里运气不太好。正如建议的那样,我尝试创建一个数组来保存信息框,但我一定做错了什么。

我尝试添加以下声明:

ibArray = [];

然后我尝试添加:

ibArray.push(marker); 

我已经在几个位置添加了上述内容,包括在将标记推入 newMarkers 数组之后立即添加,但无论我将代码放在哪里,它都会破坏我的其余代码并且不显示任何信息框。

我怀疑我的语法不正确,而且我也不知道在哪里放置代码。

其他人可以帮忙吗?

4

1 回答 1

1

您应该对标记执行相同的操作,即跟踪 infoWindows 并循环遍历数组,关闭它们(并将它们从数组中删除)。

于 2012-06-04T07:10:36.290 回答