且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果我在此处单击其他标记,如何删除以前的信息气泡?

更新时间:2023-08-29 16:24:58

您可以在添加新的信息气泡之前从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);
}