88 * @param {H.service.Platform } platform A stub class to access HERE services
99 */
1010function calculateRouteFromAtoB ( platform ) {
11- var router = platform . getRoutingService ( ) ,
12- routeRequestParams = {
13- mode : 'shortest;pedestrian' ,
14- representation : 'display' ,
15- waypoint0 : '51.51326,-0.0968752' , // St Paul's Cathedral
16- waypoint1 : '51.5081,-0.0985' , // Tate Modern
17- routeattributes : 'waypoints,summary,shape,legs' ,
18- maneuverattributes : 'direction,action'
19- } ;
11+ var router = platform . getRoutingService ( null , 8 ) ,
12+ routeRequestParams = {
13+ routingMode : 'fast' ,
14+ transportMode : 'pedestrian' ,
15+ origin : '51.51326,-0.0968752' , // St Paul's Cathedral
16+ destination : '51.5081,-0.0985' , // Tate Modern
17+ return : 'polyline,turnByTurnActions,actions,instructions,travelSummary'
18+ } ;
2019
2120
2221 router . calculateRoute (
@@ -32,18 +31,16 @@ function calculateRouteFromAtoB (platform) {
3231 * see: http://developer.here.com/rest-apis/documentation/routing/topics/resource-type-calculate-route.html
3332 */
3433function onSuccess ( result ) {
35- var route = result . response . route [ 0 ] ;
34+ var route = result . routes [ 0 ] ;
3635 /*
3736 * The styling of the route response on the map is entirely under the developer's control.
3837 * A representitive styling can be found the full JS + HTML code of this example
3938 * in the functions below:
4039 */
4140 addRouteShapeToMap ( route ) ;
4241 addManueversToMap ( route ) ;
43-
44- addWaypointsToPanel ( route . waypoint ) ;
4542 addManueversToPanel ( route ) ;
46- addSummaryToPanel ( route . summary ) ;
43+ addSummaryToPanel ( route ) ;
4744 // ... etc.
4845}
4946
@@ -116,26 +113,24 @@ function openBubble(position, text){
116113 * @param {Object } route A route as received from the H.service.RoutingService
117114 */
118115function addRouteShapeToMap ( route ) {
119- var lineString = new H . geo . LineString ( ) ,
120- routeShape = route . shape ,
121- polyline ;
122-
123- routeShape . forEach ( function ( point ) {
124- var parts = point . split ( ',' ) ;
125- lineString . pushLatLngAlt ( parts [ 0 ] , parts [ 1 ] ) ;
126- } ) ;
127-
128- polyline = new H . map . Polyline ( lineString , {
129- style : {
130- lineWidth : 4 ,
131- strokeColor : 'rgba(0, 128, 255, 0.7)'
132- }
133- } ) ;
134- // Add the polyline to the map
135- map . addObject ( polyline ) ;
136- // And zoom to its bounding rectangle
137- map . getViewModel ( ) . setLookAtData ( {
138- bounds : polyline . getBoundingBox ( )
116+ route . sections . forEach ( ( section ) => {
117+ // decode LineString from the flexible polyline
118+ let linestring = H . geo . LineString . fromFlexiblePolyline ( section . polyline ) ;
119+
120+ // Create a polyline to display the route:
121+ let polyline = new H . map . Polyline ( linestring , {
122+ style : {
123+ lineWidth : 4 ,
124+ strokeColor : 'rgba(0, 128, 255, 0.7)'
125+ }
126+ } ) ;
127+
128+ // Add the polyline to the map
129+ map . addObject ( polyline ) ;
130+ // And zoom to its bounding rectangle
131+ map . getViewModel ( ) . setLookAtData ( {
132+ bounds : polyline . getBoundingBox ( )
133+ } ) ;
139134 } ) ;
140135}
141136
@@ -154,65 +149,50 @@ function addManueversToMap(route){
154149 group = new H . map . Group ( ) ,
155150 i ,
156151 j ;
152+ route . sections . forEach ( ( section ) => {
153+ let poly = H . geo . LineString . fromFlexiblePolyline ( section . polyline ) . getLatLngAltArray ( ) ;
157154
158- // Add a marker for each maneuver
159- for ( i = 0 ; i < route . leg . length ; i += 1 ) {
160- for ( j = 0 ; j < route . leg [ i ] . maneuver . length ; j += 1 ) {
161- // Get the next maneuver.
162- maneuver = route . leg [ i ] . maneuver [ j ] ;
163- // Add a marker to the maneuvers group
155+ let actions = section . actions ;
156+ // Add a marker for each maneuver
157+ for ( i = 0 ; i < actions . length ; i += 1 ) {
158+ let action = actions [ i ] ;
164159 var marker = new H . map . Marker ( {
165- lat : maneuver . position . latitude ,
166- lng : maneuver . position . longitude } ,
160+ lat : poly [ action . offset * 3 ] ,
161+ lng : poly [ action . offset * 3 + 1 ] } ,
167162 { icon : dotIcon } ) ;
168- marker . instruction = maneuver . instruction ;
163+ marker . instruction = action . instruction ;
169164 group . addObject ( marker ) ;
170165 }
171- }
172166
173- group . addEventListener ( 'tap' , function ( evt ) {
174- map . setCenter ( evt . target . getGeometry ( ) ) ;
175- openBubble (
176- evt . target . getGeometry ( ) , evt . target . instruction ) ;
177- } , false ) ;
167+ group . addEventListener ( 'tap' , function ( evt ) {
168+ map . setCenter ( evt . target . getGeometry ( ) ) ;
169+ openBubble (
170+ evt . target . getGeometry ( ) , evt . target . instruction ) ;
171+ } , false ) ;
178172
179- // Add the maneuvers group to the map
180- map . addObject ( group ) ;
173+ // Add the maneuvers group to the map
174+ map . addObject ( group ) ;
175+ } ) ;
181176}
182177
183178
184179/**
185180 * Creates a series of H.map.Marker points from the route and adds them to the map.
186181 * @param {Object } route A route as received from the H.service.RoutingService
187182 */
188- function addWaypointsToPanel ( waypoints ) {
183+ function addSummaryToPanel ( route ) {
184+ let duration = 0 ,
185+ distance = 0 ;
189186
187+ route . sections . forEach ( ( section ) => {
188+ distance += section . travelSummary . length ;
189+ duration += section . travelSummary . duration ;
190+ } ) ;
190191
191-
192- var nodeH3 = document . createElement ( 'h3' ) ,
193- waypointLabels = [ ] ,
194- i ;
195-
196-
197- for ( i = 0 ; i < waypoints . length ; i += 1 ) {
198- waypointLabels . push ( waypoints [ i ] . label )
199- }
200-
201- nodeH3 . textContent = waypointLabels . join ( ' - ' ) ;
202-
203- routeInstructionsContainer . innerHTML = '' ;
204- routeInstructionsContainer . appendChild ( nodeH3 ) ;
205- }
206-
207- /**
208- * Creates a series of H.map.Marker points from the route and adds them to the map.
209- * @param {Object } route A route as received from the H.service.RoutingService
210- */
211- function addSummaryToPanel ( summary ) {
212192 var summaryDiv = document . createElement ( 'div' ) ,
213193 content = '' ;
214- content += '<b>Total distance</b>: ' + summary . distance + 'm. <br/>' ;
215- content += '<b>Travel Time</b>: ' + summary . travelTime . toMMSS ( ) + ' (in current traffic)' ;
194+ content += '<b>Total distance</b>: ' + distance + 'm. <br/>' ;
195+ content += '<b>Travel Time</b>: ' + duration . toMMSS ( ) ;
216196
217197
218198 summaryDiv . style . fontSize = 'small' ;
@@ -227,36 +207,27 @@ function addSummaryToPanel(summary){
227207 * @param {Object } route A route as received from the H.service.RoutingService
228208 */
229209function addManueversToPanel ( route ) {
230-
231-
232-
233- var nodeOL = document . createElement ( 'ol' ) ,
234- i ,
235- j ;
210+ var nodeOL = document . createElement ( 'ol' ) ;
236211
237212 nodeOL . style . fontSize = 'small' ;
238213 nodeOL . style . marginLeft = '5%' ;
239214 nodeOL . style . marginRight = '5%' ;
240215 nodeOL . className = 'directions' ;
241216
242- // Add a marker for each maneuver
243- for ( i = 0 ; i < route . leg . length ; i += 1 ) {
244- for ( j = 0 ; j < route . leg [ i ] . maneuver . length ; j += 1 ) {
245- // Get the next maneuver.
246- maneuver = route . leg [ i ] . maneuver [ j ] ;
247-
217+ route . sections . forEach ( ( section ) => {
218+ section . actions . forEach ( ( action , idx ) => {
248219 var li = document . createElement ( 'li' ) ,
249- spanArrow = document . createElement ( 'span' ) ,
250- spanInstruction = document . createElement ( 'span' ) ;
220+ spanArrow = document . createElement ( 'span' ) ,
221+ spanInstruction = document . createElement ( 'span' ) ;
251222
252- spanArrow . className = 'arrow ' + maneuver . action ;
253- spanInstruction . innerHTML = maneuver . instruction ;
223+ spanArrow . className = 'arrow ' + ( action . direction || '' ) + action . action ;
224+ spanInstruction . innerHTML = section . actions [ idx ] . instruction ;
254225 li . appendChild ( spanArrow ) ;
255226 li . appendChild ( spanInstruction ) ;
256227
257228 nodeOL . appendChild ( li ) ;
258- }
259- }
229+ } ) ;
230+ } ) ;
260231
261232 routeInstructionsContainer . appendChild ( nodeOL ) ;
262233}
0 commit comments