|
| 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); |
0 commit comments