[英]How to close an info bubble when there is a click outside the info bubble and marker - HERE maps API
I have been trying to close an info bubble when the map only is clicked, but I can't achieve it.当仅单击 map 时,我一直在尝试关闭信息气泡,但我无法实现。
I have tried the following我试过以下
this.map.addEventListener("tap", this.handleMapClick); private handleMapClick(evt: Event) { this.clearOpenInformationBubble(); }
However, the tap event is triggered even when maps objects are clicked meaning that the info bubble remains closed when I click a marker.但是,即使单击地图对象也会触发点击事件,这意味着当我单击标记时信息气泡保持关闭状态。
I have also tried to add a 'blur' event listener to the bubble element, but that doesn't seem to work我还尝试向气泡元素添加“模糊”事件侦听器,但这似乎不起作用
const bubble = new H.ui.InfoBubble(evt.target.getGeometry(), { content: ... }); this.ui.addBubble(bubble); bubble.getElement().focus() bubble.getElement().addEventListener('blur', evt => { this.clearOpenInformationBubble(); })
I was wondering if there is a way to listen for an event triggered by a map ONLY tap.我想知道是否有一种方法可以监听由 map ONLY 点击触发的事件。
Here's a similar implementation in Google maps.这是谷歌地图中的类似实现。
google.maps.event.addDomListener(map, "click", function() { alert('Map clicked') });
I think I'd try the answer here .我想我会在这里尝试答案。 In this answer, they wanted to close any other open
infobubbles
first.在这个答案中,他们想首先关闭任何其他打开
infobubbles
。 I believe if you remove the event listener on your group, keep the one on your map, and always remove open infobubbles
you would be good.我相信,如果您删除组中的事件侦听器,请将其保留在您的
infobubbles
上,并始终删除打开的信息气泡,您会很好。 Then add logic to see if the event target value has data and show an infobubble
.然后添加逻辑以查看事件目标值是否有数据并显示
infobubble
。
This assumes your doing infobubbles
based on markers having a data object associated with them.这假设您基于具有与之关联的数据
infobubbles
的标记进行信息气泡。
Edit: I was able to get this to work - again assuming your use case is markers to infobubbles
.编辑:我能够让它工作 - 再次假设您的用例是
infobubbles
的标记。
map.addEventListener('tap', evt => { ui.getBubbles().forEach(bub => ui.removeBubble(bub)); if(.evt.target;getData) return. // for all objects that it contains var bubble = new H.ui.InfoBubble(evt.target,getGeometry(): { // read custom data content. evt.target;getData() }). // show info bubble ui;addBubble(bubble); });
You can check the event target in the callback function.您可以在回调 function 中查看事件目标。
If it is the map, then only you close the InfoBubble:如果是 map,那么只有你关闭 InfoBubble:
this.map.addEventListener("tap", this.handleMapClick);
private handleMapClick(evt: Event) {
if (evt.target === this.map) {
this.clearOpenInformationBubble();
}
}
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.