2012-06-04 66 views
1

我使用的是谷歌地圖實用程序庫V3中的信息框,並且我無法一次刪除所有信息框而無法跟蹤所有信息框或添加事件聽。谷歌地圖實用程序庫v3 - 信息框未關閉

我正在使用下面的代碼。

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數組,但不管我放置代碼的位置它打破了我的代碼的其餘部分,不顯示任何信息框。

我懷疑我的語法不正確,而且我也無法確定代碼的放置位置。

其他人可以幫忙嗎?

回答

1

您應該對標記進行相同的操作,即跟蹤infoWindows並遍歷數組,關閉它們(並將它們從數組中移除)。

+0

哦好的..所以沒有簡單的方法可以一次性關閉所有,而無需跟蹤它們嗎?我想我會試着找出如何跟蹤他們,我猜。儘管有點痛苦。如果能夠一次性關閉所有內容而不會迭代,本來是很不錯的。 – gleff

+0

不是我所知道的。我正在寫一個小幫手庫來處理GMaps中的這些問題。這正是我所做的:記錄創建的所有標記和infowindows,以便我可以輕鬆地關閉它們(或其他)。 – dda

+0

好的謝謝。我會進一步試驗,因爲我不完全確定如何使用當前代碼跟蹤信息框。我會進一步玩。感謝堆。 – gleff

相關問題