Show Div content when hovering over HERE Maps

I'm new to maps and need to show a div on a marker pointer. I was able to place markers with icons, but now you need to show a div with additional information. Does this API Map API provide this functionality? Any document url or code snippet would be appreciated. NOTE. I am using HERE maps JS API for web.

+3


source to share


4 answers


I managed to find the correct mouse over events for the HERE map markers, which are pointer and pointerleave , and a sample code for using these events:



// After Initializing map with your own credentials.
var map = new H.Map(document.getElementById('map'),
      defaultLayers.normal.map,{
      center: {lat: LAT_VAL, lng: LNG_VAL},
      zoom: 12
    });

var domMarker = new H.map.DomMarker(coords, {
            icon: domIcon
          });
var bubble;
domMarker.addEventListener('pointerenter', function(evt) {
              bubble = new H.ui.InfoBubble({lat:"SOME_VALUE",lng:"SOME_VALUE"}, {
              content: "Your content come here"
            });
            ui.addBubble(bubble);
          }, false);
          domMarker.addEventListener('pointerleave', function(evt) {
            bubble.close();
          }, false);
map.addObject(domMarker);

      

+4


source


You can create a tooltip effect by adding various event listeners to the map to check if the mouse is over an object.

(function (ctx) {
  // ensure CSS is injected
  var tooltipStyleNode = ctx.createElement('style'),
    css = '#nm_tooltip{' +
      ' color:white;' +
      ' background:black;' +
      ' border: 1px solid grey;' +
      ' padding-left: 1em; ' +
      ' padding-right: 1em; ' +
      ' display: none;  ' +
      ' min-width: 120px;  ' +
      '}';

  tooltipStyleNode.type = 'text/css';
  if (tooltipStyleNode.styleSheet) { // IE
    tooltipStyleNode.styleSheet.cssText = css;
  } else {
    tooltipStyleNode.appendChild(ctx.createTextNode(css));
  }
  if (ctx.body) {
    ctx.body.appendChild(tooltipStyleNode);
  } else if (ctx.addEventListener) {
    ctx.addEventListener('DOMContentLoaded',  function () {
      ctx.body.appendChild(tooltipStyleNode);
    }, false);
  } else {
    ctx.attachEvent('DOMContentLoaded',  function () {
      ctx.body.appendChild(tooltipStyleNode);
    });
  }
})(document);


Object.defineProperty(Tooltip.prototype, 'visible', {
  get: function() {
    return this._visible;
  },
  set: function(visible) {
    this._visible = visible;
    this.tooltip.style.display = visible ? 'block' : 'none';
  }
});


function Tooltip(map) {
  var that = this;
  that.map = map;
  that.tooltip  = document.createElement('div');
  that.tooltip.id = 'nm_tooltip';
  that.tooltip.style.position = 'absolute';
  obj = null,
  showTooltip = function () {
    var point = that.map.geoToScreen(obj.getPosition()),
      left = point.x - (that.tooltip.offsetWidth / 2),
      top = point.y + 1; // Slight offset to avoid flicker.
    that.tooltip.style.left = left + 'px';
    that.tooltip.style.top = top + 'px';
    that.visible = true;
    that.tooltip.innerHTML =  obj.title;
  };


  map.getElement().appendChild(that.tooltip);
  map.addEventListener('pointermove', function (evt) {
    obj = that.map.getObjectAt(evt.currentPointer.viewportX,
        evt.currentPointer.viewportY);
    if(obj && obj.title){
      showTooltip();
    } else {
      that.visible = false;
    }
  });

  map.addEventListener('tap', function (evt){
    that.tooltip.visible  = false;
  });
  map.addEventListener('drag', function (evt){
    if (that.visible) {
      showTooltip();
    }
  });
};

      

This is initialized by passing an object map

as shown:



function addTooltipControlToMap(map) {
  tooltip = new Tooltip(map);
}

      

The code injected looks for an attribute .title

to be added to map objects - this can be updated to use .getData()

if needed. Tooltips can be initialized as shown below using text or html:

function addMarkersWithTooltips(map) {

    // Simple Marker with tooltip
  var brandenburgerTorMarker = new H.map.Marker(
    {lat:52.516237, lng: 13.35}),
    fernsehturmMarker = new H.map.Marker(
      {lat:52.520816, lng:13.409417});

  brandenburgerTorMarker.title = 'Brandenburger Tor';

  // Marker with HTML Tooltip
  fernsehturmMarker.title ='<div>' +
    '<h2>Tooltip with HTML content<\/h2>' +
    '<img width=\'120\' height=90 src=' +
    '\'http://upload.wikimedia.org/wikipedia/commons/' +
    '8/84/Berlin-fernsehturm.JPG\' ' +
    'alt=\'\'/><br/><b>Fernsehturm, Berlin<\/b>' +
    '<\/div>';

  // Add the markers onto the map
  map.addObjects([brandenburgerTorMarker, fernsehturmMarker]);
}

      

+2


source


You don't have a hover listener for the marker, but you can show the infoBubble on click

http://heremaps.github.io/examples/explorer.html#infobubble-on-marker-click

If that doesn't work for you, you'll have to use jquery and attach the hover to the HTML marker element. (This is not an easy task)

+1


source


Depending on the api version you are using, you may find what you are looking for in the pdf documentation (or at least start from now on). Suppose you need to render some HTML style markers , you might need:

Anyway, if you need to show information about a marker, I would suggest using the InfoBubbles that were designed for this purpose. In docs 3.0.5 :

// Create an info bubble object at a specific geographic location:
ui = H.ui.UI.createDefault(self.map, defaultLayers);
var bubble = new H.ui.InfoBubble({ lng: 13.4, lat: 52.51 }, {
 content: '<b>Hello World!</b>'
 });

// Add info bubble to the UI:
ui.addBubble(bubble);

      

To show them, you must attach an event to the tap event of the marker:

marker.addEventListener('tap', function (evt) {

    //create and add the bubble

}

      

Anyway, you can find the documentation for your api version here: https://developer.here.com/documentation/versions

+1


source







All Articles