44 * @param {H.service.Platform } platform A stub class to access HERE services
55 */
66function calculateRouteFromAtoB ( platform ) {
7- var router = platform . getRoutingService ( ) ,
7+ var router = platform . getRoutingService ( null , 8 ) ,
88 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
9+ routingMode : 'fast' ,
10+ transportMode : 'bicycle' ,
11+ origin : '-16.1647142,-67.7229166' ,
12+ destination : '-16.3705847,-68.0452683' ,
13+ return : 'polyline,elevation' // explicitly request altitude data
1614 } ;
1715
1816 router . calculateRoute (
@@ -27,60 +25,62 @@ function calculateRouteFromAtoB (platform) {
2725 * H.map.Marker
2826 */
2927function 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 ;
28+ var route = result . routes [ 0 ] ;
29+ route . sections . forEach ( ( section ) => {
30+ let lineString = H . geo . LineString . fromFlexiblePolyline ( section . polyline ) ,
31+ group = new H . map . Group ( ) ,
32+ dict = { } ,
33+ polyline ;
34+
35+ let coords = lineString . getLatLngAltArray ( ) ;
36+
37+ for ( let i = 2 , len = coords . length ; i < len ; i += 3 ) {
38+ let elevation = coords [ i ] ;
39+
40+ // normalize the altitude values for the color range
41+ var p = ( elevation - 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 : coords [ i - 2 ] , lng : coords [ i - 1 ] , alt : elevation
65+ } , { icon : icon } ) ;
66+ group . addObject ( marker ) ;
6067 }
6168
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 : 2 ,
72- strokeColor : '#555555'
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
69+ polyline = new H . map . Polyline ( lineString , {
70+ style : {
71+ lineWidth : 2 ,
72+ strokeColor : '#555555'
73+ }
74+ } ) ;
75+ // Add the polyline to the map
76+ map . addObject ( polyline ) ;
77+ // Add markers to the map
78+ map . addObject ( group ) ;
79+ // Zoom to its bounding rectangle
80+ map . getViewModel ( ) . setLookAtData ( {
81+ bounds : polyline . getBoundingBox ( ) ,
82+ tilt : 60
83+ } ) ;
8484 } ) ;
8585}
8686
0 commit comments