Skip to content

Commit 01a053f

Browse files
authored
Merge pull request #69 from kplotnik/master
Markers with altitude demo
2 parents 9bf8567 + b3ad312 commit 01a053f

File tree

6 files changed

+197
-0
lines changed

6 files changed

+197
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ All of the following examples use **version 3.1** of the API
3434
* [Marker Clustering](https://heremaps.github.io/maps-api-for-javascript-examples/marker-clustering/demo.html) - Cluster multiple markers together to better visualize the data
3535
* [Marker Clustering with Custom Theme](https://heremaps.github.io/maps-api-for-javascript-examples/custom-cluster-theme/demo.html) - Cluster multiple markers and customize the theme
3636
* [Marker on the Map](https://heremaps.github.io/maps-api-for-javascript-examples/markers-on-the-map/demo.html) - Display a map highlighting points of interest
37+
* [Markers with Altitude](https://heremaps.github.io/maps-api-for-javascript-examples/markers-with-altitude/demo.html) - Display markers at different altitudes
3738
* [Multi-language support](https://heremaps.github.io/maps-api-for-javascript-examples/map-multi-language-support/demo.html) - Display a map with labels in a foreign language
3839
* [Opening an Infobubble on a Mouse Click](https://heremaps.github.io/maps-api-for-javascript-examples/open-infobubble/demo.html) - Open an infobubble when a marker is clicked
3940
* [Ordering Overlapping Markers](https://heremaps.github.io/maps-api-for-javascript-examples/ordering-overlapping-markers/demo.html) - Arrange the order in which a series of map objects are displayed

markers-with-altitude/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+
}

markers-with-altitude/demo.details

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: Markers with altitude
3+
description: Display markers at different altitudes
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+
...

markers-with-altitude/demo.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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>Markers with altitude</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>Markers with altitude</h1>
20+
<p>Display markers at different altitudes.</p>
21+
</div>
22+
<p>This example shows the bicyle route at the Yungas Road (also known as the "Death road") in Bolivia. Markers
23+
with the altitude values help to visualise the 3500m of descent for this road.
24+
</p>
25+
<div id="map"></div>
26+
<h3>Code</h3>
27+
<p>Access to the routing service is obtained from the <code>H.service.Platform</code> by calling
28+
<code>getRoutingService()</code>. The <code>calculateRoute()</code> method is used to calculate the bicycle route by passing in the relevant parameters as defined in
29+
<a href="http://developer.here.com/rest-apis/documentation/routing/topics/resource-calculate-route.html" target="_blank">Routing API</a>.
30+
The markers are supplied with the latitude, longitude and altitude obtained from the routing response, the altitude value provides the elevation for the marker.</p>
31+
<script type="text/javascript" src='demo.js'></script>
32+
</body>
33+
</html>

markers-with-altitude/demo.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
2+
/**
3+
* Calculate the bicycle route.
4+
* @param {H.service.Platform} platform A stub class to access HERE services
5+
*/
6+
function calculateRouteFromAtoB (platform) {
7+
var router = platform.getRoutingService(),
8+
routeRequestParams = {
9+
mode: 'fastest;bicycle',
10+
representation: 'display',
11+
routeattributes : 'shape',
12+
waypoint0: '-16.1647142,-67.7229166',
13+
waypoint1: '-16.3705847,-68.0452683',
14+
// explicitly request altitude values
15+
returnElevation: true
16+
};
17+
18+
router.calculateRoute(
19+
routeRequestParams,
20+
onSuccess,
21+
onError
22+
);
23+
}
24+
25+
/**
26+
* Process the routing response and visualise the descent with the help of the
27+
* H.map.Marker
28+
*/
29+
function onSuccess(result) {
30+
var lineString = new H.geo.LineString(),
31+
routeShape = result.response.route[0].shape,
32+
group = new H.map.Group(),
33+
dict = {},
34+
polyline;
35+
36+
routeShape.forEach(function(point) {
37+
var parts = point.split(',');
38+
lineString.pushLatLngAlt(parts[0], parts[1]);
39+
40+
// normalize the altitude values for the color range
41+
var p = (parts[2] - 1000) / (4700 - 1000);
42+
var r = Math.round(255 * p);
43+
var b = Math.round(255 - 255 * p);
44+
45+
// create or re-use icon
46+
var icon;
47+
if (dict[r + '_' + b]) {
48+
icon = dict[r + '_' + b];
49+
} else {
50+
var canvas = document.createElement('canvas');
51+
canvas.width = 4;
52+
canvas.height = 4;
53+
54+
var ctx = canvas.getContext('2d');
55+
ctx.fillStyle = 'rgb(' + r + ', 0, ' + b + ')';
56+
ctx.fillRect(0, 0, 4, 4);
57+
icon = new H.map.Icon(canvas);
58+
// cache the icon for the future reuse
59+
dict[r + '_' + b] = icon;
60+
}
61+
62+
// the marker is placed at the provided altitude
63+
var marker = new H.map.Marker({
64+
lat: parts[0], lng: parts[1], alt: parts[2]
65+
}, {icon: icon});
66+
group.addObject(marker);
67+
});
68+
69+
polyline = new H.map.Polyline(lineString, {
70+
style: {
71+
lineWidth: 3,
72+
strokeColor: '#999999'
73+
}
74+
});
75+
76+
// Add the polyline to the map
77+
map.addObject(polyline);
78+
// Add markers to the map
79+
map.addObject(group);
80+
// Zoom to its bounding rectangle
81+
map.getViewModel().setLookAtData({
82+
bounds: polyline.getBoundingBox(),
83+
tilt: 60
84+
});
85+
}
86+
87+
/**
88+
* This function will be called if a communication error occurs during the JSON-P request
89+
* @param {Object} error The error message received.
90+
*/
91+
function onError(error) {
92+
alert('Can\'t reach the remote server');
93+
}
94+
95+
/**
96+
* Boilerplate map initialization code starts below:
97+
*/
98+
99+
// set up containers for the map + panel
100+
var mapContainer = document.getElementById('map'),
101+
routeInstructionsContainer = document.getElementById('panel');
102+
103+
//Step 1: initialize communication with the platform
104+
// In your own code, replace variable window.apikey with your own apikey
105+
var platform = new H.service.Platform({
106+
apikey: window.apikey
107+
});
108+
109+
var defaultLayers = platform.createDefaultLayers();
110+
111+
//Step 2: initialize a map - this map is centered over Berlin
112+
var map = new H.Map(mapContainer,
113+
defaultLayers.vector.normal.map,{
114+
center: {lat:52.5160, lng:13.3779},
115+
zoom: 13,
116+
pixelRatio: window.devicePixelRatio || 1
117+
});
118+
// add a resize listener to make sure that the map occupies the whole container
119+
window.addEventListener('resize', () => map.getViewPort().resize());
120+
121+
//Step 3: make the map interactive
122+
// MapEvents enables the event system
123+
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
124+
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
125+
126+
// Create the default UI components
127+
var ui = H.ui.UI.createDefault(map, defaultLayers);
128+
129+
130+
131+
132+
// Now use the map as required...
133+
calculateRouteFromAtoB (platform);

meta.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@
173173
"title": "Draggable Marker",
174174
"shortDescription": "Display a moveable marker on a map",
175175
"published": true
176+
},
177+
{
178+
"uid": "markers-with-altitude",
179+
"title": "Markers with Altitude",
180+
"shortDescription": "Display markers at different altitudes",
181+
"published": true
176182
}
177183
]
178184
},

0 commit comments

Comments
 (0)