2

I'm after the travel times between a set of nodes (in both directions) including traffic. In the old version of the API (7.2) I would use the following code in Python to request this:

    payload = {
        "apiKey": "API_KEY_HERE",
        "start0": "-33.795602,151.185366",
        "start1": "-33.865103,151.205627",
        "destination0": "-33.795602,151.185366",
        "destination1": "-33.865103,151.205627",
        "mode": "fastest;car;traffic:enabled",
        "departure": "2020-10-27T08:00:00+11",
        "summaryattributes": "all"
    }
    base_url = "https://matrix.route.ls.hereapi.com/routing/7.2/calculatematrix.json?"
    r = requests.get(base_url, params=payload)
    print(r.json())

The new version has fewer examples, and to be honest I'm not so familiar with POSTs and Asynchronous responses.

Why change to the new version? Well, it seems that you could only feed a single set of origin nodes/locations and then a matrix will be computed (in the background) and once ready it can be pulled with a GET request. No specifying start0, start1, ..etc

New try with the version 8:

Steps:

  1. Request matrix (POST)
  2. Poll for status (GET)
  3. Download result when ready (GET)
# 1. Request matrix (POST)
    base_url = "https://matrix.router.hereapi.com/v8/matrix?"
    params = {"apiKey": "MY_API_KEY_HERE",
              "async": "true",
              "departureTime": "2020-10-27T08:00:00+11"}
    payload = {"origins": [{"lat": -33.759688, "lng": 151.156369}, {"lat": -33.865189, "lng": 151.208162},
                           {"lat": -33.677066, "lng": 151.302117}],
               "regionDefinition": {"type": "autoCircle", "margin": 10000},
               "matrixAttributes": ["travelTimes", "distances"]}
    headers = {'Content-Type': 'application/json'}
    r = requests.post(base_url, params=params, json=payload, headers=headers)
    response = r.json()
    
    # pretty print
    print(json.dumps(response, indent=4))

This gives an "accepted" status:

    // Example response
    {
        "matrixId": "eba6780c-0379-40f1-abaf-5c62d07dabb4",
        "status": "accepted",
        "statusUrl": "https://aws-eu-west-1.matrix.router.hereapi.com/v8/matrix/eba6780c-0379-40f1-abaf-5c62d07dabb4/status"
    }

Then I use the statusUrl and my apiKey to poll the status. This is where I'm stuck. How should I authenticate? I'm not sure how the authentication should work. The authentication in step 1 worked.

# 2. Poll for status (GET)
    time.sleep(3)
    statusUrl = response['statusUrl']
    params = {'apiKey': 'MY_API_KEY_HERE'}
    headers = {'Content-Type': 'application/json'}
    r = requests.get(statusUrl, params=params, headers=headers)
    response = r.json()
    
    # pretty print
    print(json.dumps(response, indent=4))

Where 'MY_API_KEY_HERE' is written I input my apiKey. The response:

{
    "error": "Unauthorized",
    "error_description": "No credentials found"
}

Evidently, there is an error using the authentication. How should the authentication be used? Would it be possible to show how a successful request for checking the status of a submitted matrix calculation looks like and how the request for downloading such matrix looks like in Python (the next step after polling the status using gzip headers)?

Any pointers would be welcome.

  • Did you manage to authenticate the async request?? – Lucas Bastos Jan 06 '21 at 19:48
  • Ran into the same issue with async authorization using the apiKey doesn't work for the second step fetching the statusUrl. The only resolution I could find (at this point) was to switch fully to bearer tokens. – DanTheMan Jan 18 '21 at 22:27
  • Can you share the correct code to do the request using POST? I'm also having trouble using this API in Python. – brenda Mar 03 '21 at 00:39

1 Answers1

1

It looks like based on the documentation, when you provide departure time, they no longer take live traffic into account - I'm not sure about historical though. So if you need live traffic taken into account, you will have to remove departure time.

This will calculate travel times including traffic time: Method URL: https://matrix.router.hereapi.com/v8/matrix?apiKey={{API_KEY}}&async=false

Method: POST

Body:

{
    "origins": [
        {
            "lat": 47.673993,
            "lng": -122.356108
        },
        {
            "lat": 47.656910,
            "lng": -122.362823
        },
        {
            "lat": 47.648015,
            "lng": -122.335674
        },
        {
            "lat": 47.653022,
            "lng": -122.312461
        },
        {
            "lat":47.675796,
            "lng": -122.311520
        }
    ],
    "destinations": [
        {
            "lat": 47.661438,
            "lng": -122.336427
        }
    ],
    "regionDefinition": {
        "type": "world"
    }
}
  • Great answer, it helped to see I wasn't aware to differentiate between the _parameters_ in the URL and JSON in the body. However, to get the travel time including the traffic the regionDefention "world" cannot be used. Per definition the departuretime is "any", which makes it traffic insensitive. To reflect the new issue I'll update the initial question. – Peter Nijhuis Oct 20 '20 at 09:06