src/providers/routes/routes.service.js
module.exports = HereMapsRoutesService;
HereMapsRoutesService.$inject = ['$q', 'HereMapsMarkerService'];
function HereMapsRoutesService($q, HereMapsMarkerService) {
return {
calculateRoute: calculateRoute,
addRouteToMap: addRouteToMap,
cleanRoutes: cleanRoutes
}
function calculateRoute(heremaps, config) {
var platform = heremaps.platform,
map = heremaps.map,
router = platform.getRoutingService(),
dir = config.direction,
waypoints = dir.waypoints;
var mode = '{{MODE}};{{VECHILE}}'
.replace(/{{MODE}}/, dir.mode || 'fastest')
.replace(/{{VECHILE}}/, config.driveType);
var routeRequestParams = {
mode: mode,
representation: dir.representation || 'display',
language: dir.language || 'en-gb'
};
waypoints.forEach(function (waypoint, i) {
routeRequestParams["waypoint" + i] = [waypoint.lat, waypoint.lng].join(',');
});
_setAttributes(routeRequestParams, dir.attrs);
var deferred = $q.defer();
router.calculateRoute(routeRequestParams, function (result) {
deferred.resolve(result);
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
}
function cleanRoutes(map) {
var group = map.routesGroup;
if (!group)
return;
group.removeAll();
map.removeObject(group);
map.routesGroup = null;
}
function addRouteToMap(map, routeData, clean) {
if (clean)
cleanRoutes(map);
var route = routeData.route;
if (!map || !route || !route.shape)
return;
var strip = new H.geo.Strip(), polyline = null;
route.shape.forEach(function (point) {
var parts = point.split(',');
strip.pushLatLngAlt(parts[0], parts[1]);
});
var style = routeData.style || {};
polyline = new H.map.Polyline(strip, {
style: {
lineWidth: style.lineWidth || 4,
strokeColor: style.color || 'rgba(0, 128, 255, 0.7)'
}
});
var group = map.routesGroup;
if (!group) {
group = map.routesGroup = new H.map.Group();
map.addObject(group);
}
group.addObject(polyline);
if(routeData.zoomToBounds) {
HereMapsMarkerService.setViewBounds(map, polyline.getBounds(), true);
}
}
//#region PRIVATE
function _setAttributes(params, attrs) {
var _key = 'attributes';
for (var key in attrs) {
if (!attrs.hasOwnProperty(key))
continue;
params[key + _key] = attrs[key];
}
}
/**
* Creates a series of H.map.Marker points from the route and adds them to the map.
* @param {Object} route A route as received from the H.service.RoutingService
*/
function addManueversToMap(map, route) {
var svgMarkup = '<svg width="18" height="18" ' +
'xmlns="http://www.w3.org/2000/svg">' +
'<circle cx="8" cy="8" r="8" ' +
'fill="#1b468d" stroke="white" stroke-width="1" />' +
'</svg>',
dotIcon = new H.map.Icon(svgMarkup, { anchor: { x: 8, y: 8 } }),
group = new H.map.Group(), i, j;
// Add a marker for each maneuver
for (i = 0; i < route.leg.length; i += 1) {
for (j = 0; j < route.leg[i].maneuver.length; j += 1) {
// Get the next maneuver.
maneuver = route.leg[i].maneuver[j];
// Add a marker to the maneuvers group
var marker = new H.map.Marker({
lat: maneuver.position.latitude,
lng: maneuver.position.longitude
},
{ icon: dotIcon }
);
marker.instruction = maneuver.instruction;
group.addObject(marker);
}
}
group.addEventListener('tap', function (evt) {
map.setCenter(evt.target.getPosition());
openBubble(evt.target.getPosition(), evt.target.instruction);
}, false);
// Add the maneuvers group to the map
map.addObject(group);
}
/**
* Creates a series of H.map.Marker points from the route and adds them to the map.
* @param {Object} route A route as received from the H.service.RoutingService
*/
function addWaypointsToPanel(waypoints) {
var nodeH3 = document.createElement('h3'),
waypointLabels = [],
i;
for (i = 0; i < waypoints.length; i += 1) {
waypointLabels.push(waypoints[i].label)
}
nodeH3.textContent = waypointLabels.join(' - ');
routeInstructionsContainer.innerHTML = '';
routeInstructionsContainer.appendChild(nodeH3);
}
/**
* Creates a series of H.map.Marker points from the route and adds them to the map.
* @param {Object} route A route as received from the H.service.RoutingService
*/
function addSummaryToPanel(summary) {
var summaryDiv = document.createElement('div'),
content = '';
content += '<b>Total distance</b>: ' + summary.distance + 'm. <br/>';
content += '<b>Travel Time</b>: ' + summary.travelTime.toMMSS() + ' (in current traffic)';
summaryDiv.style.fontSize = 'small';
summaryDiv.style.marginLeft = '5%';
summaryDiv.style.marginRight = '5%';
summaryDiv.innerHTML = content;
routeInstructionsContainer.appendChild(summaryDiv);
}
/**
* Creates a series of H.map.Marker points from the route and adds them to the map.
* @param {Object} route A route as received from the H.service.RoutingService
*/
function addManueversToPanel(route) {
var nodeOL = document.createElement('ol'), i, j;
nodeOL.style.fontSize = 'small';
nodeOL.style.marginLeft = '5%';
nodeOL.style.marginRight = '5%';
nodeOL.className = 'directions';
// Add a marker for each maneuver
for (i = 0; i < route.leg.length; i += 1) {
for (j = 0; j < route.leg[i].maneuver.length; j += 1) {
// Get the next maneuver.
maneuver = route.leg[i].maneuver[j];
var li = document.createElement('li'),
spanArrow = document.createElement('span'),
spanInstruction = document.createElement('span');
spanArrow.className = 'arrow ' + maneuver.action;
spanInstruction.innerHTML = maneuver.instruction;
li.appendChild(spanArrow);
li.appendChild(spanInstruction);
nodeOL.appendChild(li);
}
}
routeInstructionsContainer.appendChild(nodeOL);
}
};