2011-08-01 59 views
0

我需要清除從Google地圖功能創建的DIV。 有兩個HTML DIV。我遇到的DIV是MarkerCountJavascript谷歌地圖在地圖上清除HTML復位功能

<div id="sidebar"></div> 
<div id="MarkerCount"></div> 

function createMarkerCount(count) { 
var div = document.createElement('div'); 
var html = "Results: " + count + " Facilities"; 
div.innerHTML = html; 
div.style.marginBottom = '5px'; 
return div; 
} 

這是在標記返回到Google地圖時創建的。這基本上是對當前在地圖上顯示的記錄數進行計數。此功能正常工作。

這裏是我的功能重新映射並清除標記和另一innerHTML的DIV的工具條:

function clearLocations() { 
infoWindow.close(); 
for (var i = 0; i < markers.length; i++) { 
markers[i].setMap(null); 
} 
markers.length = 0; 
sidebar.innerHTML = ""; 
map.setCenter(new google.maps.LatLng(36.1611, -116.4775), 6); 
} 

此功能就好了。現在,當我想清楚了MarkerCount, 我想下面的行添加到函數clearLocations()

MarkerCount.innerHTML = ""; 

然而,當第一次加載頁面,你執行搜索時,clearLocations()函數被調用在開始搜索之前,因爲目前沒有爲CreateMarker函數創建的innerhtml。這會拋出一個錯誤,指出CreateMarker爲NULL,因爲沒有任何內容。

因此,瘦身是,sidebar.innerhtml得到清除很好,但是當我添加代碼行以清除CreateMarker.innerhtml DIV時,我收到一個錯誤。不知道爲什麼這可能發生....

編輯 - 函數來顯示CountMarker如何獲取計數

var markercountEntry = createMarkerCount(markerNodes.length); 
MarkerCount.appendChild(markercountEntry); 
+0

編輯你的問題,以顯示CreateMarker是如何設置的。 – mrk

+0

謝謝,我已經加了 –

回答

1

這聽起來像您有沒有被定義的元素上設置的innerHTML問題但在您的clearLocations()函數中。我建議你只處理CreateMarkerMarkerCount尚未定義的情況。

喜歡的東西:

function clearLocations() { 
    if(CreateMarker && CreateMarker.innerHTML) 
     CreateMarker.innerHTML = ""; 
    infoWindow.close(); 
    for (var i = 0; i < markers.length; i++) { 
    markers[i].setMap(null); 
    } 
    markers.length = 0; 
    sidebar.innerHTML = ""; 
    map.setCenter(new google.maps.LatLng(36.1611, -116.4775), 6); 
} 
+0

非常感謝! –