Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当用户按escape键时,如何在google地图上隐藏标记?_Javascript_Google Maps - Fatal编程技术网

Javascript 当用户按escape键时,如何在google地图上隐藏标记?

Javascript 当用户按escape键时,如何在google地图上隐藏标记?,javascript,google-maps,Javascript,Google Maps,使用谷歌地图3.44.10,我在谷歌地图上显示标记,我想隐藏标记 当用户单击标记外或按escape时 定义glovar变量 markerInfoWindow : null, // hide marker when user clicked out of marker - it works ok this.locationMap.addListener('click', function (event) { if (sel

使用谷歌地图3.44.10,我在谷歌地图上显示标记,我想隐藏标记 当用户单击标记外或按escape时

定义glovar变量

    markerInfoWindow : null,


        // hide marker when user clicked out of marker - it works ok
        this.locationMap.addListener('click', function (event) {
                if (self.markerInfoWindow) {
                    self.markerInfoWindow.close()
                    self.markerInfoWindow = null
                }
        });



        google.maps.event.addListener(self.locationMarker, 'click', (function (marker, i) {
            return function () {
                self.markerInfoWindow = new google.maps.InfoWindow();
                let content = ‘CONTENT TEXT’

                self.markerInfoWindow.setContent(content); // I set content for marker and show it
                self.markerInfoWindow.open(self.locationMap, self.locationMarker);

                // Try to catch key events but - failed events are not triggered!
                google.maps.event.addDomListener(self.markerInfoWindow, 'keyup keypress', function (e) {
                    console.log('keyup keypress e::')
                    console.log(e)
                    var code = (e.keyCode ? e.keyCode : e.which);
                    if (code === 27) {
                        if (self.markerInfoWindow) {
                            self.markerInfoWindow.close()
                            self.markerInfoWindow = null
                        }
                    }
                });

            }
        })(this.locationMarker));
正确程度如何


谢谢

只需调用InfoWindow上的
close()
方法即可关闭InfoWindow,如中所述

要在退出时关闭信息窗口,只需监听
keyup
事件,检查键是否为“退出”,如果是,则关闭信息窗口。同样,要在单击时关闭信息窗口,请侦听
click
事件

示例(如果代码段未正确加载):

函数初始化(){
让myLatLng=new google.maps.LatLng(40.71,-74.00),
myOptions={
缩放:5,
中心:myLatLng,
mapTypeId:google.maps.mapTypeId.ROADMAP
},
map=new google.maps.map(document.getElementById('map-canvas'),myOptions),
marker=新的google.maps.marker({
职位:myLatLng,
地图:地图
});
const infoWindow=new google.maps.infoWindow({
内容:“废话废话”,
});
marker.setMap(map);
//关闭地图上的信息窗口单击
marker.addListener(“单击”,()=>{
信息窗口。打开(地图、标记);
})
//按escape键关闭信息窗口
document.body.addEventListener(“keyup”,(e)=>{
如果(e.key==“Escape”){
infoWindow.close();
}
});
map.addListener(“单击”,()=>{
infoWindow.close();
})
}
初始化()
#地图画布{
高度:300px;
宽度:600px;
背景色:#CCC;
}


您已将按键侦听器嵌套在标记单击侦听器中。。。我认为应该将它移出marker click listener并非第一次要求您提供…@Abronsius教授,markerInfoWindow是在用户单击marker时创建的,这就是我在listener中设置代码的原因。我不知道如何以其他方式实现它?下面@JoshG的示例非常有效。
markerInfoWindow
initialize
函数的范围内全局可用-因此这是一种方法。另一个是按照您当前代码的思路-看起来您已经创建了一个map对象类类型函数(我想我们只能看到其中的一部分),并将map、infowindow指定为该对象的属性。。。这是另一种方式