本文介绍了如果我在此处单击其他标记,如何删除以前的信息气泡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的添加信息气泡的代码.在其他标记上单击(点击)后,我想先删除当前的信息气泡.

Here below is my code to add an infobubble. I want to remove the current infobubble previously after click(tap) on a different marker.

function addInfoBubble(map) 
    {
    var group = new H.map.Group();
    map.addObject(group);
    // add 'tap' event listener, that opens info bubble, to the group
    group.addEventListener('tap', function(evt) {
    // event target is the marker itself, group is a parent event target
    // for all objects that it contains
    var bubble = new H.ui.InfoBubble(evt.target.getPosition(), {
    // read custom data
    content: evt.target.getData()
    });
    // show info bubble
    ui.addBubble(bubble);
    }, false);
    }

如果我单击其他标记,应该将代码行ui.removeBubble(bubble)放在哪里?

Where should I place the code line ui.removeBubble(bubble) if I click on a different marker?

推荐答案

您可以在添加新的信息气泡之前从ui中删除所有信息气泡.如果您不介意删除所有当前显示的气泡,则应该执行以下操作:

You could remove all infobubbles from ui just before adding a new one. If you don't mind removing all current bubbles being displayed, something like this should work:

ui.getBubbles().forEach(bub => ui.removeBubble(bub));

将其合并到您的代码中:

Incorporating it to your code:

function addInfoBubble(map) {
   var group = new H.map.Group();
   map.addObject(group);
   // add 'tap' event listener, that opens info bubble, to the group
   group.addEventListener('tap', function(evt) {
      // event target is the marker itself, group is a parent event target
      // for all objects that it contains
      var bubble = new H.ui.InfoBubble(evt.target.getPosition(), {
        // read custom data
        content: evt.target.getData()
      });
      //remove infobubbles
      ui.getBubbles().forEach(bub => ui.removeBubble(bub));

      // show info bubble
      ui.addBubble(bubble);
   }, false);
}

这篇关于如果我在此处单击其他标记,如何删除以前的信息气泡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 04:32