2017-08-10 184 views
0

好吧即時嘗試做的是添加一個重置按鈕到我的谷歌地圖的頂部。它是如此,當你打電話的方向,並希望擺脫目前的方向,或者只是清除這裏的出發點在地圖和recenter是使用刪除一個事件監聽器

controlUI.addEventListener('click', function() { 
 
      map.setCenter(pyrmont), 
 
\t \t map.setZoom(14), 
 
\t \t directionsDisplay.setPanel(null), 
 
\t \t directionsDisplay.setMap(null); \t \t \t \t 
 
     });

和它所有的事件偵聽器的IM工作正常,但按下'重置'按鈕後,你不能再做任何的方向,我猜測這是因爲我調用了我的兩個方向var的.setPanel和.setMap,並將它們都設置爲null,但它沒有清除它們之後,問題是一個簡單的刪除事件偵聽器的工作?!如果是這樣,我怎麼會寫出來我曾嘗試過幾種方法,他們都沒有正常工作。任何幫助或指針都會非常有幫助。

謝謝你在前進,

特拉維斯

哦,這裏是我在這裏呼籲

function CenterControl(controlDiv, map) { 
 

 
     // Set CSS for the control border. 
 
     var controlUI = document.createElement('div'); 
 
     controlUI.style.backgroundColor = '#fff'; 
 
     controlUI.style.border = '2px solid #fff'; 
 
     controlUI.style.borderRadius = '3px'; 
 
     controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)'; 
 
     controlUI.style.cursor = 'pointer'; 
 
     controlUI.style.marginBottom = '22px'; 
 
     controlUI.style.textAlign = 'center'; 
 
     controlUI.title = 'Click to reset the map'; 
 
     controlDiv.appendChild(controlUI); 
 

 
     // Set CSS for the control interior. 
 
     var controlText = document.createElement('div'); 
 
     controlText.style.color = 'rgb(25,25,25)'; 
 
     controlText.style.fontFamily = 'Roboto,Arial,sans-serif'; 
 
     controlText.style.fontSize = '16px'; 
 
     controlText.style.lineHeight = '38px'; 
 
     controlText.style.paddingLeft = '5px'; 
 
     controlText.style.paddingRight = '5px'; 
 
     controlText.innerHTML = 'Reset Map'; 
 
     controlUI.appendChild(controlText); 
 

 
     // Setup the click event listeners: simply set the map to default. 
 
     controlUI.addEventListener('click', function() { 
 
      map.setCenter(pyrmont), 
 
\t \t map.setZoom(14), 
 
\t \t directionsDisplay.setPanel(null), 
 
\t \t directionsDisplay.setMap(null); \t \t \t \t 
 
     }); 
 
     }

回答

0

的controlUI功能是否有一個理由罵null和不只是將地圖重置爲您最初呈現的內容?我認爲這個SO貼子會幫助你。

Google map reset to initial state

0

所以我做只是平了召回/重新復位功能本身的地方服務和方向渲染

controlUI.addEventListener('click', function() { 
 
\t \t \t map.setCenter(pyrmont), 
 
\t \t \t map.setZoom(14), 
 
\t \t \t directionsDisplay.setMap(null); 
 
\t \t \t directionsDisplay.setPanel(null); 
 
\t \t \t service = new google.maps.places.PlacesService(map); 
 
\t \t \t directionsDisplay = new google.maps.DirectionsRenderer; 
 
\t \t \t directionsDisplay.setMap(map); 
 
\t \t \t directionsDisplay.setPanel(document.getElementById('directionsPanel')); 
 
\t \t \t directionsService = new google.maps.DirectionsService; \t 
 
\t \t });

感謝您的幫助。