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