1-
21/**
32 * Calculates and displays a car route from the Brandenburg Gate in the centre of Berlin
43 * to Friedrichstraße Railway Station.
54 *
65 * A full list of available request parameters can be found in the Routing API documentation.
7- * see: http://developer.here.com/rest-apis/documentation/routing/topics/resource-calculate-route.html
6+ * see: http://developer.here.com/rest-apis/documentation/routing/topics/resource-calculate-route.html
87 *
9- * @param {H.service.Platform } platform A stub class to access HERE services
8+ * @param {H.service.Platform } platform A stub class to access HERE services
109 */
11- function calculateRouteFromAtoB ( platform ) {
10+ function calculateRouteFromAtoB ( platform ) {
1211 var router = platform . getRoutingService ( null , 8 ) ,
1312 routeRequestParams = {
1413 routingMode : 'fast' ,
1514 transportMode : 'car' ,
1615 origin : '52.5160,13.3779' , // Brandenburg Gate
17- destination : '52.5206,13.3862' , // Friedrichstraße Railway Station
16+ destination : '52.5206,13.3862' , // Friedrichstraße Railway Station
1817 return : 'polyline,turnByTurnActions,actions,instructions,travelSummary'
1918 } ;
2019
21-
2220 router . calculateRoute (
2321 routeRequestParams ,
2422 onSuccess ,
2523 onError
2624 ) ;
2725}
26+
2827/**
2928 * This function will be called once the Routing REST API provides a response
30- * @param {Object } result A JSONP object representing the calculated route
29+ * @param {Object } result A JSONP object representing the calculated route
3130 *
3231 * see: http://developer.here.com/rest-apis/documentation/routing/topics/resource-type-calculate-route.html
3332 */
3433function onSuccess ( result ) {
3534 var route = result . routes [ 0 ] ;
36- /*
37- * The styling of the route response on the map is entirely under the developer's control.
38- * A representitive styling can be found the full JS + HTML code of this example
39- * in the functions below:
40- */
35+
36+ /*
37+ * The styling of the route response on the map is entirely under the developer's control.
38+ * A representative styling can be found the full JS + HTML code of this example
39+ * in the functions below:
40+ */
4141 addRouteShapeToMap ( route ) ;
4242 addManueversToMap ( route ) ;
4343 addWaypointsToPanel ( route ) ;
@@ -48,7 +48,7 @@ function onSuccess(result) {
4848
4949/**
5050 * This function will be called if a communication error occurs during the JSON-P request
51- * @param {Object } error The error message received.
51+ * @param {Object } error The error message received.
5252 */
5353function onError ( error ) {
5454 alert ( 'Can\'t reach the remote server' ) ;
@@ -58,29 +58,30 @@ function onError(error) {
5858 * Boilerplate map initialization code starts below:
5959 */
6060
61- // set up containers for the map + panel
61+ // set up containers for the map + panel
6262var mapContainer = document . getElementById ( 'map' ) ,
6363 routeInstructionsContainer = document . getElementById ( 'panel' ) ;
6464
65- //Step 1: initialize communication with the platform
65+ // Step 1: initialize communication with the platform
6666// In your own code, replace variable window.apikey with your own apikey
6767var platform = new H . service . Platform ( {
6868 apikey : window . apikey
6969} ) ;
7070
7171var defaultLayers = platform . createDefaultLayers ( ) ;
7272
73- //Step 2: initialize a map - this map is centered over Berlin
73+ // Step 2: initialize a map - this map is centered over Berlin
7474var map = new H . Map ( mapContainer ,
75- defaultLayers . vector . normal . map , {
76- center : { lat :52.5160 , lng :13.3779 } ,
75+ defaultLayers . vector . normal . map , {
76+ center : { lat : 52.5160 , lng : 13.3779 } ,
7777 zoom : 13 ,
7878 pixelRatio : window . devicePixelRatio || 1
7979} ) ;
80+
8081// add a resize listener to make sure that the map occupies the whole container
8182window . addEventListener ( 'resize' , ( ) => map . getViewPort ( ) . resize ( ) ) ;
8283
83- //Step 3: make the map interactive
84+ // Step 3: make the map interactive
8485// MapEvents enables the event system
8586// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
8687var behavior = new H . mapevents . Behavior ( new H . mapevents . MapEvents ( map ) ) ;
@@ -93,12 +94,12 @@ var bubble;
9394
9495/**
9596 * Opens/Closes a infobubble
96- * @param {H.geo.Point } position The location on the map.
97- * @param {String } text The contents of the infobubble.
97+ * @param {H.geo.Point } position The location on the map.
98+ * @param {String } text The contents of the infobubble.
9899 */
99- function openBubble ( position , text ) {
100- if ( ! bubble ) {
101- bubble = new H . ui . InfoBubble (
100+ function openBubble ( position , text ) {
101+ if ( ! bubble ) {
102+ bubble = new H . ui . InfoBubble (
102103 position ,
103104 // The FO property holds the province name.
104105 { content : text } ) ;
@@ -110,12 +111,11 @@ function openBubble(position, text){
110111 }
111112}
112113
113-
114114/**
115115 * Creates a H.map.Polyline from the shape of the route and adds it to the map.
116116 * @param {Object } route A route as received from the H.service.RoutingService
117117 */
118- function addRouteShapeToMap ( route ) {
118+ function addRouteShapeToMap ( route ) {
119119 route . sections . forEach ( ( section ) => {
120120 // decode LineString from the flexible polyline
121121 let linestring = H . geo . LineString . fromFlexiblePolyline ( section . polyline ) ;
@@ -137,29 +137,29 @@ function addRouteShapeToMap(route){
137137 } ) ;
138138}
139139
140-
141140/**
142141 * Creates a series of H.map.Marker points from the route and adds them to the map.
143- * @param {Object } route A route as received from the H.service.RoutingService
142+ * @param {Object } route A route as received from the H.service.RoutingService
144143 */
145- function addManueversToMap ( route ) {
144+ function addManueversToMap ( route ) {
146145 var svgMarkup = '<svg width="18" height="18" ' +
147146 'xmlns="http://www.w3.org/2000/svg">' +
148147 '<circle cx="8" cy="8" r="8" ' +
149- 'fill="#1b468d" stroke="white" stroke-width="1" />' +
148+ 'fill="#1b468d" stroke="white" stroke-width="1" />' +
150149 '</svg>' ,
151150 dotIcon = new H . map . Icon ( svgMarkup , { anchor : { x :8 , y :8 } } ) ,
152- group = new H . map . Group ( ) ,
151+ group = new H . map . Group ( ) ,
153152 i ,
154153 j ;
154+
155155 route . sections . forEach ( ( section ) => {
156156 let poly = H . geo . LineString . fromFlexiblePolyline ( section . polyline ) . getLatLngAltArray ( ) ;
157157
158158 let actions = section . actions ;
159159 // Add a marker for each maneuver
160- for ( i = 0 ; i < actions . length ; i += 1 ) {
160+ for ( i = 0 ; i < actions . length ; i += 1 ) {
161161 let action = actions [ i ] ;
162- var marker = new H . map . Marker ( {
162+ var marker = new H . map . Marker ( {
163163 lat : poly [ action . offset * 3 ] ,
164164 lng : poly [ action . offset * 3 + 1 ] } ,
165165 { icon : dotIcon } ) ;
@@ -169,67 +169,63 @@ function addManueversToMap(route){
169169
170170 group . addEventListener ( 'tap' , function ( evt ) {
171171 map . setCenter ( evt . target . getGeometry ( ) ) ;
172- openBubble (
173- evt . target . getGeometry ( ) , evt . target . instruction ) ;
172+ openBubble ( evt . target . getGeometry ( ) , evt . target . instruction ) ;
174173 } , false ) ;
175174
176175 // Add the maneuvers group to the map
177176 map . addObject ( group ) ;
178177 } ) ;
179178}
180179
181-
182180/**
183181 * Creates a series of H.map.Marker points from the route and adds them to the map.
184- * @param {Object } route A route as received from the H.service.RoutingService
182+ * @param {Object } route A route as received from the H.service.RoutingService
185183 */
186184function addWaypointsToPanel ( route ) {
187185 var nodeH3 = document . createElement ( 'h3' ) ,
188- labels = [ ] ;
186+ labels = [ ] ;
189187
190188 route . sections . forEach ( ( section ) => {
191189 labels . push (
192190 section . turnByTurnActions [ 0 ] . nextRoad . name [ 0 ] . value )
193191 labels . push (
194192 section . turnByTurnActions [ section . turnByTurnActions . length - 1 ] . currentRoad . name [ 0 ] . value )
195193 } ) ;
196-
194+
197195 nodeH3 . textContent = labels . join ( ' - ' ) ;
198196 routeInstructionsContainer . innerHTML = '' ;
199197 routeInstructionsContainer . appendChild ( nodeH3 ) ;
200198}
201199
202200/**
203201 * Creates a series of H.map.Marker points from the route and adds them to the map.
204- * @param {Object } route A route as received from the H.service.RoutingService
202+ * @param {Object } route A route as received from the H.service.RoutingService
205203 */
206- function addSummaryToPanel ( route ) {
204+ function addSummaryToPanel ( route ) {
207205 let duration = 0 ,
208- distance = 0 ;
206+ distance = 0 ;
209207
210208 route . sections . forEach ( ( section ) => {
211209 distance += section . travelSummary . length ;
212210 duration += section . travelSummary . duration ;
213211 } ) ;
214212
215213 var summaryDiv = document . createElement ( 'div' ) ,
216- content = '' ;
217- content += '<b>Total distance</b>: ' + distance + 'm. <br/>' ;
218- content += '<b>Travel Time</b>: ' + duration . toMMSS ( ) + ' (in current traffic)' ;
219-
214+ content = '<b>Total distance</b>: ' + distance + 'm. <br />' +
215+ '<b>Travel Time</b>: ' + toMMSS ( duration ) + ' (in current traffic)' ;
220216
221217 summaryDiv . style . fontSize = 'small' ;
222- summaryDiv . style . marginLeft = '5%' ;
223- summaryDiv . style . marginRight = '5%' ;
218+ summaryDiv . style . marginLeft = '5%' ;
219+ summaryDiv . style . marginRight = '5%' ;
224220 summaryDiv . innerHTML = content ;
225221 routeInstructionsContainer . appendChild ( summaryDiv ) ;
226222}
227223
228224/**
229225 * Creates a series of H.map.Marker points from the route and adds them to the map.
230- * @param {Object } route A route as received from the H.service.RoutingService
226+ * @param {Object } route A route as received from the H.service.RoutingService
231227 */
232- function addManueversToPanel ( route ) {
228+ function addManueversToPanel ( route ) {
233229 var nodeOL = document . createElement ( 'ol' ) ;
234230
235231 nodeOL . style . fontSize = 'small' ;
@@ -240,8 +236,8 @@ function addManueversToPanel(route){
240236 route . sections . forEach ( ( section ) => {
241237 section . actions . forEach ( ( action , idx ) => {
242238 var li = document . createElement ( 'li' ) ,
243- spanArrow = document . createElement ( 'span' ) ,
244- spanInstruction = document . createElement ( 'span' ) ;
239+ spanArrow = document . createElement ( 'span' ) ,
240+ spanInstruction = document . createElement ( 'span' ) ;
245241
246242 spanArrow . className = 'arrow ' + ( action . direction || '' ) + action . action ;
247243 spanInstruction . innerHTML = section . actions [ idx ] . instruction ;
@@ -255,10 +251,9 @@ function addManueversToPanel(route){
255251 routeInstructionsContainer . appendChild ( nodeOL ) ;
256252}
257253
258-
259- Number . prototype . toMMSS = function ( ) {
260- return Math . floor ( this / 60 ) + ' minutes ' + ( this % 60 ) + ' seconds.' ;
254+ function toMMSS ( duration ) {
255+ return Math . floor ( duration / 60 ) + ' minutes ' + ( duration % 60 ) + ' seconds.' ;
261256}
262257
263258// Now use the map as required...
264- calculateRouteFromAtoB ( platform ) ;
259+ calculateRouteFromAtoB ( platform ) ;
0 commit comments