I was wondering whether anyone could help me figure out how to receive the distance between two waypoints information from Here Maps using a Data poller written in js.
The following is the query function:
function SetOptions(objectDict) {
routeOptions = {
uri: 'https://route.api.here.com/routing/7.2/calculateroute.json?',
qs: {
waypoint0: 'geo!' + objectDict["startLatitude"] + "," + objectDict["startLongitude"],
waypoint1: 'geo!' 'stopover!' + objectDict["StopOverPoint1Latitude"] + "," + objectDict["StopOverPoint1Longitude"],
waypoint2: 'geo!' 'stopover!' + objectDict["StopOverPoint2Latitude"] + "," + objectDict["StopOverPoint2Longitude"], //waypoint as stopoverpoint can be added here
waypoint3: 'geo!' + objectDict["destinationLatitude"] + "," + objectDict["destinationLongitude"],
mode: 'fastest;car;traffic:enabled',
jsonAttributes: 1,
linkAttributes: 'sh,sl,ds', // shape, speedlimit, dynamic speed info
routeAttributes: 'waypoints,shape,boundingBox',
legAttributes: 'boundingBox,shape',
returnElevation: true, //to recieve Elevation information along with lat & long
maneuverAttributes: 'po,sh,rn,sp,rs,sa,sq',
representation: 'turnByTurn',
app_id: 'xxx',
app_code: 'xxxxx'
}
}
startHereRequest(routeOptions);
}
While the following collects the return information of the API
function startHereRequest(option) {
request(option, function (error, response, body) {
var data = JSON.parse(body);
var longitudes = [];
var latitudes = [];
var speedLimits = [];
var jamFactors = [];
var trafficTimes = [];
var trafficSpeeds = [];
var elevation = [];
var baseSpeeds = [];
var baseTimes = [];
//(Beware of same Name )
if (data.response !== undefined) {
data.response.route[0].leg.forEach(legs => {
legs.link.forEach(links => {
speedLimits.push(links.speedLimit);
var geo = links.shape[0].split(",");
latitudes.push(parseFloat(geo[0]));
longitudes.push(parseFloat(geo[1]));
elevation.push(parseFloat(geo[2]));
jamFactors.push(links.dynamicSpeedInfo.jamFactor);
// api documentation to recieve values (push);
trafficTimes.push(links.dynamicSpeedInfo.trafficTime);
trafficSpeeds.push(links.dynamicSpeedInfo.trafficSpeed);
baseSpeeds.push(links.dynamicSpeedInfo.baseSpeed);
baseTimes.push(links.dynamicSpeedInfo.baseTime);
});
});
var result = { longitudes: longitudes, latitudes: latitudes, speedLimits: speedLimits, jamFactors: jamFactors, trafficTimes: trafficTimes, elevation:elevation, trafficSpeeds:trafficSpeeds, baseSpeeds:baseSpeeds, baseTimes:baseTimes };
SetObject("PM/ds/waypointsLatitude", latitudes);
SetObject("PM/ds/waypointsLongitude", longitudes);
SetObject("PM/ds/waypointsSpeedlimit", speedLimits);
SetObject("PM/ds/waypointsTrafficTimes", trafficTimes);
SetObject("PM/ds/waypointsJamFactors", jamFactors);
SetObject("PM/ds/waypointsGradient", elevation);
SetObject("PM/ds/waypointsBaseSpeed", baseSpeeds);
SetObject("PM/ds/waypointsBaseTime", baseTimes);
SetObject("PM/ds/waypointsTrafficSpeed", trafficSpeeds);
console.log(latitudes);
// fs.writeFileSync("./results/results.json", JSON.stringify(result));
// fs.writeFileSync("./results/rawdata.json", JSON.stringify(data.response.route[0]));
} else {
console.log(error);
console.log(response);
}
});
}
I already have an offline function using the Haversine formula to calculate the distance between two waypoints, however it seems like there is a possibility to provide this information directly from Here Maps.