|
| 1 | +/** |
| 2 | + * A full list of available request parameters can be found in the Routing API documentation. |
| 3 | + * see: http://developer.here.com/rest-apis/documentation/routing/topics/resource-calculate-route.html |
| 4 | + */ |
| 5 | +var routeRequestParams = { |
| 6 | + mode: 'fastest;truck', |
| 7 | + waypoint0: 'geo!40.7249546323,-74.0110042', // Manhattan |
| 8 | + waypoint1: 'geo!40.7324386599,-74.0341396', // Newport |
| 9 | + representation: 'display', |
| 10 | + routeattributes : 'waypoints,summary,shape,legs', |
| 11 | + 'metricSystem': 'imperial' |
| 12 | +}; |
| 13 | + |
| 14 | +function calculateRoutes(platform) { |
| 15 | + var router = platform.getRoutingService(); |
| 16 | + |
| 17 | + // The blue route showing a simple truck route |
| 18 | + calculateRoute(router, routeRequestParams, { |
| 19 | + strokeColor: 'rgba(0, 128, 255, 0.7)', |
| 20 | + lineWidth: 10 |
| 21 | + }); |
| 22 | + |
| 23 | + // The green route showing a truck route with a trailer |
| 24 | + calculateRoute(router, Object.assign(routeRequestParams, { |
| 25 | + trailersCount: 1 |
| 26 | + }), { |
| 27 | + strokeColor: 'rgba(25, 150, 10, 0.7)', |
| 28 | + lineWidth: 7 |
| 29 | + }); |
| 30 | + |
| 31 | + // The violet route showing a truck route with a trailer |
| 32 | + calculateRoute(router, Object.assign(routeRequestParams, { |
| 33 | + trailersCount: 1, |
| 34 | + shippedHazardousGoods: 'flammable' |
| 35 | + }), { |
| 36 | + strokeColor: 'rgba(255, 0, 255, 0.7)', |
| 37 | + lineWidth: 5 |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Calculates and displays a route. |
| 43 | + * @param {Object} params The Routing API request parameters |
| 44 | + * @param {H.service.RoutingService} router The service stub for requesting the Routing API |
| 45 | + * @param {mapsjs.map.SpatialStyle.Options} style The style of the route to display on the map |
| 46 | + */ |
| 47 | +function calculateRoute (router, params, style) { |
| 48 | + router.calculateRoute(params, function(result) { |
| 49 | + addRouteShapeToMap(style, result.response.route[0]); |
| 50 | + }, console.error); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Boilerplate map initialization code starts below: |
| 55 | + */ |
| 56 | + |
| 57 | +// set up containers for the map + panel |
| 58 | +var mapContainer = document.getElementById('map'), |
| 59 | + routeInstructionsContainer = document.getElementById('panel'); |
| 60 | + |
| 61 | +// Step 1: initialize communication with the platform |
| 62 | +// In your own code, replace variable window.apikey with your own apikey |
| 63 | +var platform = new H.service.Platform({ |
| 64 | + apikey: window.apikey |
| 65 | +}); |
| 66 | + |
| 67 | +var defaultLayers = platform.createDefaultLayers(); |
| 68 | + |
| 69 | +// Step 2: initialize a map - this map is centered over Berlin |
| 70 | +var map = new H.Map(mapContainer, |
| 71 | + // Set truck restriction layer as a base map |
| 72 | + defaultLayers.vector.normal.truck,{ |
| 73 | + center: {lat: 40.745390, lng: -74.022917}, |
| 74 | + zoom: 13.2, |
| 75 | + pixelRatio: window.devicePixelRatio || 1 |
| 76 | +}); |
| 77 | +// add a resize listener to make sure that the map occupies the whole container |
| 78 | +window.addEventListener('resize', () => map.getViewPort().resize()); |
| 79 | + |
| 80 | +// Step 3: make the map interactive |
| 81 | +// MapEvents enables the event system |
| 82 | +// Behavior implements default interactions for pan/zoom (also on mobile touch environments) |
| 83 | +var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map)); |
| 84 | + |
| 85 | + |
| 86 | +/** |
| 87 | + * Creates a H.map.Polyline from the shape of the route and adds it to the map. |
| 88 | + * @param {Object} route A route as received from the H.service.RoutingService |
| 89 | + */ |
| 90 | +function addRouteShapeToMap(style, route){ |
| 91 | + var lineString = new H.geo.LineString(), |
| 92 | + routeShape = route.shape, |
| 93 | + polyline, |
| 94 | + div = document.createElement('div'); |
| 95 | + |
| 96 | + div.innerHTML = route.summary.text; |
| 97 | + div.style.color = style.strokeColor; |
| 98 | + document.getElementById('panel').appendChild(div); |
| 99 | + |
| 100 | + routeShape.forEach(function(point) { |
| 101 | + var parts = point.split(','); |
| 102 | + lineString.pushLatLngAlt(parts[0], parts[1]); |
| 103 | + }); |
| 104 | + |
| 105 | + polyline = new H.map.Polyline(lineString, {style}); |
| 106 | + |
| 107 | + // Add the polyline to the map |
| 108 | + map.addObject(polyline); |
| 109 | +} |
| 110 | + |
| 111 | +// Now use the map as required... |
| 112 | +calculateRoutes (platform); |
0 commit comments