How to get distances and durations for HERE public transit api?

190 views Asked by At

I am able to retrieve some public transit route with the HERE api v8:

https://developer.here.com/documentation/public-transit/dev_guide/routing/route-example.html

And I would like to determine:

  • total distance in km
  • total duration in hours
  • total walking duration in hours

For the car routing I have the option return=summary to include length and duration. Unfortunately, that is not available for the public transit api. ('return': value is out of range).

=> Is there a similar option for the public transit api?

Or would I need to calculate that information by my own, e.g. by

  • looping over all sections,
  • transform and subtract the departure and arrival dates to get duration
  • transform polyline and estimate distances from lat,lng values
  • sum up those values for train/bus and walking sections

Part of my JavaScript code:

this.__publicTransitUrl = 'https://transit.router.hereapi.com/v8/routes';

//...

var startCoordinates = await this.__geoCode(startLocation);
var targetCoordinates = await this.__geoCode(targetLocation);

var url = `${this.__publicTransitUrl}?&origin=${startCoordinates}&destination=${targetCoordinates}&`;
if(departureTime){
  url += 'departureTime=' + departureTime + ':00&';
}
url += `return=polyline&`;
url += `apiKey=${this.__apiKey}`;

const encodedUrl = encodeURI(url);
const jsonResult = await this._jsonQuery(encodedUrl);
const route = jsonResult.routes[0];
const sections = route.sections;

//...

async __geoCode(location){
  const url = encodeURI(`${this.__geocodingUrl}?q=${location}&apiKey=${this.__apiKey}`);
  const jsonResult = await this._jsonQuery(url);
  const firstItem = jsonResult.items[0];
  const position = firstItem.position;
  const coordinatesString = '' + position.lat +',' +position.lng;
  return coordinatesString;
}

//...

Example json response:

enter image description here

0

There are 0 answers