2011-10-25 31 views
13

雖然我在完成某項任務時遇到了一些困難,但我正在嘗試採集一些我之前完成的操作。刪除在Google地圖上呈現的路線

我有一個顯示三個Google地圖的網頁。

對於這些谷歌地圖,我有接受郵編/郵政編碼的文本框,以及「獲取路線」按鈕。

點擊每個這些按鈕的使用google.maps.DirectionsService對象在一個面板在頁面的底部中心,以顯示一組的方向。

我試圖通過再次搜索來找到新路線時,出現了我的問題。正如你在下面的圖片中看到的,兩條路線都被渲染。

Two routes rendered on a map

我有一個標誌,在這在標記集結束。

我現在已經讀了幾遍你如何可以通過這個數組循環和使用marker.setMap(空)來清除該標記。

不過,我似乎無法每個特定的搜索後清除的實際路線。

有沒有人有清除多個地圖標記的問題?

您是否必須以某種方式完全重置地圖?

如果你要清楚的標記,你應該做的事一點在流程的生命週期它,這樣你的新徵程顯示的搜索之後,但在舊的被刪除?

回答

15

我用所有三個谷歌地圖一樣google.maps.DirectionsService對象,他們都調用相同的方法來計算的方向,但在自己的地圖對象作爲參數傳遞。

function calcRoute(startPoint, location_arr) { 
    // Create a renderer for directions and bind it to the map. 
    var map = location_arr[LOCATION_ARR_MAP_OBJECT]; 
    var rendererOptions = { 
    map: map 
} 

if(directionsDisplay != null) { 
    directionsDisplay.setMap(null); 
    directionsDisplay = null; 
} 

directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions); 

directionsDisplay.setMap(map); 

directionsDisplay.setPanel(document.getElementById("directionsPanel")); 

要點是如果directionsDisplay!= NULL,我們不僅傳遞null的setMap,我們也抵消了整個對象afterweards,我發現這個固定。

+0

面臨着相同的排序問題,在setMap(null)後點擊另一個引腳;在短時間內顯示舊路線,然後顯示新路線(舊路線有點眨眼)。但將舊的DirectionsRenderer設置爲空並創建新的可以解決建議的問題。 –

1

我不知道答案....最有可能所有的u需要做的是根據情況:

// put the codes after direction service is declared or run directionsService // 

directionsDisplay.setMap(null); // clear direction from the map 
directionsDisplay.setPanel(null); // clear directionpanel from the map   
directionsDisplay = new google.maps.DirectionsRenderer(); // this is to render again, otherwise your route wont show for the second time searching 
directionsDisplay.setMap(map); //this is to set up again 
1

這是你唯一需要的部分:

// Clear past routes 
    if (directionsDisplay != null) { 
     directionsDisplay.setMap(null); 
     directionsDisplay = null; 
    }