好的,我想要做的是在我的谷歌地图顶部添加一个重置按钮。这样当你调用方向并想要摆脱当前的方向或者只是清除地图并在起点上重新定位时,事件监听器就是使用
controlUI.addEventListener('click', function() {
map.setCenter(pyrmont),
map.setZoom(14),
directionsDisplay.setPanel(null),
directionsDisplay.setMap(null);
});
并且一切正常但按下'重置'按钮后你就不能再做任何指示而我猜它是因为我调用了我的两个方向var的.setPanel和.setMap并将它们都设置为null但是它的之后没有清除它们,这是一个简单的删除事件监听器工作的问题?!如果是这样我怎么写出来我已经尝试了几种方法,但它们似乎都没有正常工作。任何帮助或指示都会非常有帮助。
提前谢谢你,
特拉维斯
哦,这是我在这里调用的controlUI函数
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),
map.setZoom(14),
directionsDisplay.setPanel(null),
directionsDisplay.setMap(null);
});
}
答案 0 :(得分:0)
是否有理由调用null
,而不仅仅是将地图重置为您最初渲染的内容?我认为这篇SO帖子会对你有帮助。
答案 1 :(得分:0)
好吧,回答标题问题。要删除事件侦听器,您只需执行以下操作:
EventTarget.removeEventListener
尝试查看https://developer.mozilla.org/enUS/docs/Web/API/EventTarget/removeEventListener
我希望这会有所帮助。