5

I am very new to the Here Maps Routing API v8.

I have tried the example provided here: https://developer.here.com/documentation/maps/3.1.15.1/dev_guide/topics/routing.html

I have used the routingParameters as shown below:

var routingParameters = {
  'routingMode': 'fast',
  'transportMode': 'car',
  'origin': '50.1120,8.6834',
  'destination': '52.5309,13.3846',
  'return': 'polyline'
};

Now I want to add multiple waypoints in the routingParameters and I tried the below format for the same:

'via' : ['50.1234,8.7654', '51.2234,9.1123']

But the request is failing when I use the above line in the routingParameters.

Can you please suggest the correct format of the request with multiple waypoints ?

DXB-DEV
  • 547
  • 1
  • 7
  • 18

2 Answers2

9

Check the version of the HERE Maps JS API you are using. This is supported starting from version 3.1.19.0.

So the way to calculate a route with multiple waypoints would be:

// departure point (origin)
var start = '52.550464,13.384223';

// collection of waypoints
var waypoints = [
  '52.529791,13.401389'
  '52.513079,13.424392'
  '52.487581,13.425079'
];

// end point (destination)
var end = '52.477545,13.447395'

// routing parameters
var routingParameters = {
  'origin': start,
  'destination': end,
  'via': new H.service.Url.MultiValueQueryParameter( waypoints ),

  'routingMode': 'fast',
  'transportMode': 'car',

  'return': 'polyline'
};

// Get an instance of the routing service version 8:
var router = platform.getRoutingService(null, 8);

// Call `calculateRoute` with the routing parameters,
// the success callback and an error callback function
// The implementation of the two callback functions is left out for brevity
// see documentation link below for callback examples
router.calculateRoute(routingParameters, onResultCallback, onErrorCallback)

Calculate routes with HERE Maps JS API: documentation

Michel
  • 26,600
  • 6
  • 64
  • 69
  • 1
    Thank you for the solution. I tried this. The JS API and the Rest API works fine as mentioned above. – DXB-DEV May 30 '20 at 09:30
  • I know this is already solved, but what the Here Api expect from you is, for example via=lat,lng&via1=lat,lng&via2=lat,lng, thats what they mean when they say a list of via check https://developer.here.com/documentation/routing-api/8.7.1/api-reference-swagger.html – Bryan Acuña Núñez Sep 04 '20 at 01:28
  • This is a hard coded set of waypoints. To dynamically add them, use var waypoints = [ ]; and then waypoints.push('lat,long'); for all the waypoints you want. and replace 'via': 'lat,long' in the var routingParameters ={...}, with 'via': new H.service.Url.MultiValueQueryParameter(waypoints), – Rob Jan 24 '21 at 20:12
6

In JS, the correct way to add multiple waypoints is with H.service.Url.MultiValueQueryParameter:

var routingParameters = {
  'routingMode': 'fast',
  'transportMode': 'car',
  'origin': '50.1120,8.6834',
  'via': new H.service.Url.MultiValueQueryParameter(['50.1234,8.7654', '51.2234,9.1123']);
  'destination': '52.5309,13.3846',
  'return': 'polyline'
};

For more information check documentation at https://developer.here.com/documentation/maps/3.1.19.2/api_reference/H.service.Url.MultiValueQueryParameter.html

sunix
  • 146
  • 1
  • 8
  • This is the correct way to add the waypoints. You can use the new H.service.Url.MultiValueQueryParameter() object and dynamically push more waypoints to it as well. – Rob Jan 24 '21 at 20:11