Skip to content

Commit 1cf0a43

Browse files
authored
Merge pull request #77 from ttobis/master
2 new Examples: DOM Marker rotation & Animated Markers
2 parents 02d8a85 + 2bdd2eb commit 1cf0a43

File tree

11 files changed

+388
-3
lines changed

11 files changed

+388
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ See the [LICENSE](LICENSE) file in the root of this project for license details.
1313
All of the following examples use **version 3.1** of the API
1414

1515
* [Adding an Overlay to the Map](https://heremaps.github.io/maps-api-for-javascript-examples/custom-tile-overlay/demo.html) - Display custom map tiles as an overlay
16+
* [Animated Markers](https://heremaps.github.io/maps-api-for-javascript-examples/markers-update-position-with-animation/demo.html) - Update marker position with animation
1617
* [Calculating a Location from a Mouse Click](https://heremaps.github.io/maps-api-for-javascript-examples/position-on-mouse-click/demo.html) - Obtain the latitude and longitude of a location within the map
1718
* [Changing from the Metric System](https://heremaps.github.io/maps-api-for-javascript-examples/map-scale-bar-changing-from-the-metric-system/demo.html) - Display a map including a scale bar in miles or yards
1819
* [Circle on the Map](https://heremaps.github.io/maps-api-for-javascript-examples/circle-on-the-map/demo.html) - Display a map highlighting a circular region
1920
* [DOM Marker](https://heremaps.github.io/maps-api-for-javascript-examples/map-with-dom-marker/demo.html) - Display a marker that is capable of receiving DOM events
21+
* [DOM Marker rotation](https://heremaps.github.io/maps-api-for-javascript-examples/dom-marker-rotation/demo.html) - Rotate DOM Marker's content using CSS
2022
* [Display KML Data](https://heremaps.github.io/maps-api-for-javascript-examples/display-kml-on-map/demo.html) - Parse a KML file and display the data on a map
2123
* [Draggable Marker](https://heremaps.github.io/maps-api-for-javascript-examples/draggable-marker/demo.html) - Display a moveable marker on a map
2224
* [Draggable geo shapes](https://heremaps.github.io/maps-api-for-javascript-examples/draggable-shapes/demo.html) - Display moveable geometric shapes on a map
@@ -55,6 +57,7 @@ All of the following examples use **version 3.1** of the API
5557
* [Set a map style at the load time](https://heremaps.github.io/maps-api-for-javascript-examples/change-style-at-load/demo.html) - Set a style of the whole map during the map instantiation
5658
* [Synchronising Two Maps](https://heremaps.github.io/maps-api-for-javascript-examples/synchronising-two-maps/demo.html) - Synchronise a static map with an interactive map
5759
* [Take a Snapshot of the Map](https://heremaps.github.io/maps-api-for-javascript-examples/capture-map-area/demo.html) - Capture an area on the map
60+
* [Truck routing road restrictions](https://heremaps.github.io/maps-api-for-javascript-examples/truck-routing-road-restrictions/demo.html) Show a various truck routes with the truck related road restrictions highlighted on the map
5861
* [Zoom into Bounds](https://heremaps.github.io/maps-api-for-javascript-examples/custom-zooming-into-bounds/demo.html) - Zoom into bounds limiting maximum level
5962
* [Zooming to a Set of Markers](https://heremaps.github.io/maps-api-for-javascript-examples/zoom-to-set-of-markers/demo.html) - Alter the viewport to ensure a group of objects are visible
60-
* [Truck routing road restrictions](https://heremaps.github.io/maps-api-for-javascript-examples/truck-routing-road-restrictions/demo.html) Show a various truck routes with the truck related road restrictions highlighted on the map
63+

dom-marker-rotation/demo.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#map {
3+
width: 95%;
4+
height: 450px;
5+
background: grey;
6+
}
7+
8+
#panel {
9+
width: 100%;
10+
height: 400px;
11+
}

dom-marker-rotation/demo.details

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: DOM Marker rotation
3+
description: Rotate DOM Marker's content using CSS
4+
resources:
5+
- https://heremaps.github.io/maps-api-for-javascript-examples/test-credentials.js
6+
7+
normalize_css: no
8+
dtd: html 5
9+
wrap: d
10+
panel_html: 0
11+
panel_js: 0
12+
panel_css: 0
13+
...

dom-marker-rotation/demo.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
5+
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
6+
<title>DOM Marker rotation</title>
7+
<link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css" />
8+
<link rel="stylesheet" type="text/css" href="demo.css" />
9+
<link rel="stylesheet" type="text/css" href="../template.css" />
10+
<script type="text/javascript" src='../test-credentials.js'></script>
11+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
12+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
13+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
14+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
15+
</head>
16+
<body id="markers-on-the-map">
17+
18+
<div class="page-header">
19+
<h1>DOM Marker rotation</h1>
20+
<p>Rotate DOM Marker's content using CSS</p>
21+
</div>
22+
<p>This example shows DOM Marker with SVG icon which rotates every second by 45 degrees.
23+
</p>
24+
<div id="map"></div>
25+
<h3>Code</h3>
26+
<p>
27+
The DOM node provided in constructor of <code>H.map.DomIcon</code> is cloned and rendered every time a
28+
DOM marker reaches (is visible within) the map view port. Therefore all manipulation has to be done
29+
in <code>onAtach</code> callback where the currently displayed clone reference is available.
30+
The API manipulates position of rendered DOM element (e.g. when panning the map, zooming the map, etc.) by changing
31+
the <code>transform</code> CSS property of the <code>clonedElement</code>. Therefore in order to rotate DOM icon,
32+
we need to manipulate its child node <code>transform</code> CSS property (in our case SVG node).
33+
</p>
34+
<script type="text/javascript" src='demo.js'></script>
35+
</body>
36+
</html>

dom-marker-rotation/demo.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
/**
3+
* Create DOM Marker and rotate
4+
*/
5+
function rotateDomMarker() {
6+
var domIconElement = document.createElement('div'),
7+
interval,
8+
counter = 0;
9+
10+
// set the anchor using margin css property depending on the content's (svg element below) size
11+
// to make sure that the icon's center represents the marker's geo positon
12+
domIconElement.style.margin = '-20px 0 0 -20px';
13+
14+
// add content to the element
15+
domIconElement.innerHTML = `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="40" height="40">
16+
<path d="m0.812665,23.806608l37.937001,-22.931615l-21.749812,38.749665l1.374988,-17.749847l-17.562177,1.931797z"
17+
fill-opacity="null" stroke-opacity="null" stroke-width="1.5" stroke="#000" fill="#fff"/>
18+
</svg>`;
19+
20+
// create dom marker and add it to the map
21+
marker = map.addObject(new H.map.DomMarker({lat: 50.90978, lng: 10.87203}, {
22+
icon: new H.map.DomIcon(domIconElement, {
23+
onAttach: function(clonedElement, domIcon, domMarker) {
24+
var clonedContent = clonedElement.getElementsByTagName('svg')[0];
25+
26+
// set last used value for rotation when dom icon is attached (back in map's viewport)
27+
clonedContent.style.transform = 'rotate(' + counter + 'deg)';
28+
29+
// set interval to rotate icon's content by 45 degrees every second.
30+
interval = setInterval(function() {
31+
clonedContent.style.transform = 'rotate(' + (counter += 45) + 'deg)';
32+
}, 1000)
33+
},
34+
onDetach: function(clonedElement, domIcon, domMarker) {
35+
// stop the rotation if dom icon is not in map's viewport
36+
clearInterval(interval);
37+
}
38+
})
39+
}));
40+
}
41+
42+
/**
43+
* Boilerplate map initialization code starts below:
44+
*/
45+
46+
// set up containers for the map + panel
47+
var mapContainer = document.getElementById('map'),
48+
routeInstructionsContainer = document.getElementById('panel');
49+
50+
//Step 1: initialize communication with the platform
51+
// In your own code, replace variable window.apikey with your own apikey
52+
var platform = new H.service.Platform({
53+
apikey: window.apikey
54+
});
55+
56+
var defaultLayers = platform.createDefaultLayers();
57+
58+
//Step 2: initialize a map - this map is centered over Berlin
59+
var map = new H.Map(mapContainer,
60+
defaultLayers.vector.normal.map,{
61+
center: {lat: 50.90978, lng: 10.87203},
62+
zoom: 6,
63+
pixelRatio: (window.devicePixelRatio && window.devicePixelRatio > 1) ? 2 : 1
64+
});
65+
// add a resize listener to make sure that the map occupies the whole container
66+
window.addEventListener('resize', function () {
67+
map.getViewPort().resize();
68+
});
69+
70+
//Step 3: make the map interactive
71+
// MapEvents enables the event system
72+
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
73+
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
74+
75+
// Create the default UI components
76+
var ui = H.ui.UI.createDefault(map, defaultLayers);
77+
78+
// Now create DOM Marker and rotate it's content
79+
rotateDomMarker();

finding-the-nearest-marker/demo.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ var svgMarkup = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg">
8282
'text-anchor="middle" fill="white">${REPLACE}</text></svg>';
8383

8484
coords.forEach(function (value, index) {
85-
var myIcon = new H.map.Icon(svgMarkup.replace('${REPLACE}', index + 1)),
85+
var myIcon = new H.map.Icon(svgMarkup.replace('${REPLACE}', index + 1), {
86+
anchor: {x: 12, y: 12}
87+
}),
8688
marker = new H.map.Marker(value, {
8789
icon: myIcon,
8890
volatility: true
@@ -120,4 +122,4 @@ map.addEventListener('dragend', function(ev) {
120122
}, false);
121123

122124
// Add the click event listener.
123-
addClickEventListenerToMap(map);
125+
addClickEventListenerToMap(map);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#map {
3+
width: 95%;
4+
height: 450px;
5+
background: grey;
6+
}
7+
8+
#panel {
9+
width: 100%;
10+
height: 400px;
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: Animated markers
3+
description: Update marker position with animation
4+
resources:
5+
- https://heremaps.github.io/maps-api-for-javascript-examples/test-credentials.js
6+
7+
normalize_css: no
8+
dtd: html 5
9+
wrap: d
10+
panel_html: 0
11+
panel_js: 0
12+
panel_css: 0
13+
...
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
5+
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
6+
<title>Animated markers</title>
7+
<link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css" />
8+
<link rel="stylesheet" type="text/css" href="demo.css" />
9+
<link rel="stylesheet" type="text/css" href="../template.css" />
10+
<script type="text/javascript" src='../test-credentials.js'></script>
11+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
12+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
13+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
14+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
15+
</head>
16+
<body id="markers-on-the-map">
17+
18+
<div class="page-header">
19+
<h1>Animated markers</h1>
20+
<p>Update marker position with animation.</p>
21+
</div>
22+
<p>This example shows visualization of animated DOM markers on a map. Markers change their positions to random
23+
geo coordinates generated within radius of 450 km from the map's center.
24+
</p>
25+
<div id="map"></div>
26+
<h3>Code</h3>
27+
<p>
28+
DOM markers with SVG icon are placed around the Europe. Each DOM marker then periodicaly changes its position using
29+
<code>setGeometry()</code> method to randomly generated geo coordinate with an animation. To calculate random
30+
geo coordinate within specified radius, <code>H.geo.Point#walk()</code> method is used.
31+
The animation effect is achieved with help of custom <code>ease()</code> function which is called
32+
within <code>updateMarkerPositions()</code> function each interval loop.
33+
</p>
34+
<script type="text/javascript" src='demo.js'></script>
35+
</body>
36+
</html>

0 commit comments

Comments
 (0)