Skip to content

Commit bc9f57e

Browse files
authored
Merge pull request #115 from kplotnik/master
Isoline routing example
2 parents eeedc0e + 9952a89 commit bc9f57e

File tree

7 files changed

+208
-0
lines changed

7 files changed

+208
-0
lines changed

map-with-isoline-route/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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: Map with Isoline Route for the EV vehicle
3+
description: Request a range for the EV vehicle
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+
...

map-with-isoline-route/demo.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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>Map with Driving Route from A to B</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="styles.css" />
10+
<link rel="stylesheet" type="text/css" href="../template.css" />
11+
<script type="text/javascript" src='../test-credentials.js'></script>
12+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
13+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
14+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
15+
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
16+
<style type="text/css">
17+
.directions li span.arrow {
18+
display:inline-block;
19+
min-width:28px;
20+
min-height:28px;
21+
background-position:0px;
22+
background-image: url("https://heremaps.github.io/maps-api-for-javascript-examples/map-with-route-from-a-to-b/img/arrows.png");
23+
position:relative;
24+
top:8px;
25+
}
26+
.directions li span.depart {
27+
background-position:-28px;
28+
}
29+
.directions li span.rightturn {
30+
background-position:-224px;
31+
}
32+
.directions li span.leftturn{
33+
background-position:-252px;
34+
}
35+
.directions li span.arrive {
36+
background-position:-1288px;
37+
}
38+
</style>
39+
</head>
40+
<body id="markers-on-the-map">
41+
42+
<div class="page-header">
43+
<h1>Map with Isoline Route for the EV vehicle</h1>
44+
<p>Request a range for the EV vehicle with the given parameters and display it on the map</p>
45+
</div>
46+
<p>This example calculates the range for the EV vehicle starting its trip from the <b>Brandenburg Gate</b>
47+
in the centre of Berlin.</p>
48+
<div id="map"></div>
49+
<h3>Code</h3>
50+
<p>Access to the routing service is obtained from the <code>H.service.Platform</code> by calling
51+
<code>getRoutingService()</code>. The <code>calculateIsoline()</code> method is used to calculate the range
52+
for the vehicle with the given parameters as defined in
53+
<a href="https://developer.here.com/documentation/isoline-routing-api/dev_guide/index.html" target="_blank"> Isoline Routing API</a>.
54+
The styling and display of the response is under the control of the developer.</p>
55+
<script type="text/javascript" src='demo.js'></script>
56+
</body>
57+
</html>

map-with-isoline-route/demo.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* Calculates and displays an area reachable for the given parameters of the EV vehicle
3+
*
4+
* A full list of available request parameters can be found in the Routing API documentation.
5+
* see: https://developer.here.com/documentation/isoline-routing-api/dev_guide/topics/use-cases/consumption_based-isoline.html
6+
*
7+
* @param {H.service.Platform} platform A stub class to access HERE services
8+
*/
9+
function calculateIsolineRoute(platform) {
10+
var router = platform.getRoutingService(null, 8),
11+
routeRequestParams = {
12+
'origin': '52.51605,13.37787',
13+
'range[type]': 'consumption',
14+
'range[values]': 20000,
15+
'transportMode': 'car',
16+
'ev[freeFlowSpeedTable]': '0,0.239,27,0.239,45,0.259,60,0.196,75,0.207,90,0.238,100,0.26,110,0.296,120,0.337,130,0.351,250,0.351',
17+
'ev[trafficSpeedTable]': '0,0.349,27,0.319,45,0.329,60,0.266,75,0.287,90,0.318,100,0.33,110,0.335,120,0.35,130,0.36,250,0.36',
18+
'ev[ascent]': 9,
19+
'ev[descent]': 4.3,
20+
'ev[auxiliaryConsumption]': 1.8
21+
};
22+
23+
// add a marker to display a starting point of the vehicle
24+
map.addObject(new H.map.Marker({
25+
lat: 52.51605,
26+
lng: 13.37787
27+
}));
28+
29+
router.calculateIsoline(
30+
routeRequestParams,
31+
onSuccess,
32+
onError
33+
);
34+
}
35+
36+
/**
37+
* This function will be called once the Routing REST API provides a response
38+
* @param {Object} result A JSON object representing the calculated range
39+
*/
40+
function onSuccess(result) {
41+
var route = result.isolines[0];
42+
43+
/*
44+
* The styling of the route response on the map is entirely under the developer's control.
45+
* A representative styling can be found the full JS + HTML code of this example
46+
* in the functions below:
47+
*/
48+
addRouteShapeToMap(route);
49+
}
50+
51+
/**
52+
* This function will be called if a communication error occurs during the JSON-P request
53+
* @param {Object} error The error message received.
54+
*/
55+
function onError(error) {
56+
alert('Can\'t reach the remote server');
57+
}
58+
59+
/**
60+
* Boilerplate map initialization code starts below:
61+
*/
62+
63+
// set up containers for the map + panel
64+
var mapContainer = document.getElementById('map'),
65+
routeInstructionsContainer = document.getElementById('panel');
66+
67+
// Step 1: initialize communication with the platform
68+
// In your own code, replace variable window.apikey with your own apikey
69+
var platform = new H.service.Platform({
70+
apikey: window.apikey
71+
});
72+
73+
var defaultLayers = platform.createDefaultLayers();
74+
75+
// Step 2: initialize a map - this map is centered over Berlin
76+
var map = new H.Map(mapContainer,
77+
defaultLayers.vector.normal.map, {
78+
center: {lat: 52.5160, lng: 13.3779},
79+
zoom: 13,
80+
pixelRatio: window.devicePixelRatio || 1
81+
});
82+
83+
// add a resize listener to make sure that the map occupies the whole container
84+
window.addEventListener('resize', () => map.getViewPort().resize());
85+
86+
// Step 3: make the map interactive
87+
// MapEvents enables the event system
88+
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
89+
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
90+
91+
// Create the default UI components
92+
var ui = H.ui.UI.createDefault(map, defaultLayers);
93+
94+
/**
95+
* Creates a H.map.Polyline from the shape of the route and adds it to the map.
96+
* @param {Object} route A route as received from the H.service.RoutingService
97+
*/
98+
function addRouteShapeToMap(route) {
99+
route.polygons.forEach((section) => {
100+
// decode LineString from the flexible polyline
101+
let linestring = H.geo.LineString.fromFlexiblePolyline(section.outer);
102+
103+
// Create a polygon to display the area
104+
let polygon = new H.map.Polygon(linestring, {
105+
style: {
106+
lineWidth: 4,
107+
strokeColor: 'rgba(0, 128, 0, 0.7)'
108+
}
109+
});
110+
111+
// Add the polygon to the map
112+
map.addObject(polygon);
113+
// And zoom to its bounding rectangle
114+
map.getViewModel().setLookAtData({
115+
bounds: polygon.getBoundingBox()
116+
});
117+
});
118+
}
119+
120+
// Now use the map as required...
121+
calculateIsolineRoute(platform);
6.71 KB
Loading

map-with-isoline-route/styles.css

Whitespace-only changes.

meta.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@
395395
"shortDescription": "Request a route from A to B using public transport and display it on the map",
396396
"published": true
397397
},
398+
{
399+
"uid": "map-with-isoline-route",
400+
"title": "Map with isoline route",
401+
"shortDescription": "Request a range for the EV vehicle",
402+
"published": true
403+
},
398404
{
399405
"uid": "geocode-a-location-from-address",
400406
"title": "Search for a Location based on an Address",

0 commit comments

Comments
 (0)