How to remove previous infobubble, if I click different marker in HERE Map?

安稳与你 提交于 2019-12-10 17:26:07

问题


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);
    }

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


回答1:


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);
}


来源:https://stackoverflow.com/questions/33731921/how-to-remove-previous-infobubble-if-i-click-different-marker-in-here-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!