• JavaScript
  • HTML
                  
                  

/**
 * Create DOM Marker and rotate
 */
function rotateDomMarker() {
  var domIconElement = document.createElement('div'),
      interval,
      counter = 0;

  // set the anchor using margin css property depending on the content's (svg element below) size
  // to make sure that the icon's center represents the marker's geo positon
  domIconElement.style.margin = '-20px 0 0 -20px';

  // add content to the element
  domIconElement.innerHTML = `
      
    `;

  // create dom marker and add it to the map
  marker = map.addObject(new H.map.DomMarker({lat: 50.90978, lng: 10.87203}, {
    icon: new H.map.DomIcon(domIconElement, {
      onAttach: function(clonedElement, domIcon, domMarker) {
        var clonedContent = clonedElement.getElementsByTagName('svg')[0];

        // set last used value for rotation when dom icon is attached (back in map's viewport)
        clonedContent.style.transform = 'rotate(' + counter + 'deg)';

        // set interval to rotate icon's content by 45 degrees every second.
        interval = setInterval(function() {
          clonedContent.style.transform = 'rotate(' + (counter += 45) + 'deg)';
        }, 1000)
      },
      onDetach: function(clonedElement, domIcon, domMarker) {
        // stop the rotation if dom icon is not in map's viewport
        clearInterval(interval);
      }
    })
  }));
}

/**
 * Boilerplate map initialization code starts below:
 */

// set up containers for the map  + panel
var mapContainer = document.getElementById('map'),
  routeInstructionsContainer = document.getElementById('panel');

//Step 1: initialize communication with the platform
// In your own code, replace variable window.apikey with your own apikey
var platform = new H.service.Platform({
  apikey: window.apikey
});

var defaultLayers = platform.createDefaultLayers();

//Step 2: initialize a map - this map is centered over Berlin
var map = new H.Map(mapContainer,
  defaultLayers.vector.normal.map,{
  center: {lat: 50.90978, lng: 10.87203},
  zoom: 6,
  pixelRatio: (window.devicePixelRatio && window.devicePixelRatio > 1) ? 2 : 1
});
// add a resize listener to make sure that the map occupies the whole container
window.addEventListener('resize', function () {
  map.getViewPort().resize();
});

//Step 3: make the map interactive
// MapEvents enables the event system
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));

// Create the default UI components
var ui = H.ui.UI.createDefault(map, defaultLayers);

// Now create DOM Marker and rotate it's content
rotateDomMarker();
                  
                
                  
                  
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <title>DOM Marker rotation</title>
    <link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css" />
    <link rel="stylesheet" type="text/css" href="demo.css" />
    <link rel="stylesheet" type="text/css" href="../template.css" />
    <script type="text/javascript" src='../test-credentials.js'></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
  <script type="text/javascript" src='../js-examples-rendering-helpers/iframe-height.js'></script></head>
  <body id="markers-on-the-map">

    <div class="page-header">
        <h1>DOM Marker rotation</h1>
        <p>Rotate DOM Marker's content using CSS</p>
    </div>
    <p>This example shows DOM Marker with SVG icon which rotates every second by 45 degrees.
    </p>
    <div id="map"></div>
    <h3>Code</h3>
    <p>
      The DOM node provided in constructor of <code>H.map.DomIcon</code> is cloned and rendered every time a
      DOM marker reaches (is visible within) the map view port. Therefore all manipulation has to be done
      in <code>onAtach</code> callback where the currently displayed clone reference is available.
      The API manipulates position of rendered DOM element (e.g. when panning the map, zooming the map, etc.) by changing
      the <code>transform</code> CSS property of the <code>clonedElement</code>. Therefore in order to rotate DOM icon,
      we need to manipulate its child node <code>transform</code> CSS property (in our case SVG node).
    </p>
    <script type="text/javascript" src='demo.js'></script>
  </body>
</html>