Skip to content
Advertisement

How can I find the middle of a HERE Routing API route?

by doing a GET-Request, I am receiving routing informations from the HERE Routing API:

https://router.hereapi.com/v8/routes?apikey=MY_API_KEY&destination=52.530394,13.400683&origin=52.530728,13.383833&return=polyline,travelSummary&transportMode=truck&&vehicle[speedCap]=30&spans=names,length,duration,speedLimit

Now I want to find the coordinates for example in the middle of the route with respect to the total time. So I the example below, the total duration is 274 seconds. How can I find out, on which position I will be after 137 seconds? (In real application these times are much longer. Here, for simplicity and for a small JSON file size, I have chosen only a short distance)

First, I thought of adding starting and ending coordinates of the spans, however it seems not to be possible with the API.

Second, I thought of using the polyline. From that I receive a lot of coordinates, however I don’t see a possiblity to connect one of these coordinates to a certain duration of travel.

Is there any way how I can get the information I am looking for with the HERE Routing API or with any PHP calculation?

{
  "routes": [
    {
      "id": "90be4eb8-d0ba-47f8-9954-9be444576a17",
      "sections": [
        {
          "id": "bfd32e45-662b-4b7e-a297-21eeee09dd68",
          "type": "vehicle",
          "departure": {
            "time": "2021-12-11T23:42:04+01:00",
            "place": {
              "type": "place",
              "location": {
                "lat": 52.5307744,
                "lng": 13.3838015
              },
              "originalLocation": {
                "lat": 52.5307279,
                "lng": 13.383833
              }
            }
          },
          "arrival": {
            "time": "2021-12-11T23:46:38+01:00",
            "place": {
              "type": "place",
              "location": {
                "lat": 52.5303982,
                "lng": 13.4006967
              },
              "originalLocation": {
                "lat": 52.5303939,
                "lng": 13.4006829
              }
            }
          },
          "travelSummary": {
            "duration": 274,
            "length": 1338,
            "baseDuration": 264
          },
          "polyline": "BGslnmkDyn8wZ8CmL4Iof0F0U8BoGsEoQwCsJsEkSoBoG8BsJsE0U8BgK8BoLoB4IoB0KoBoLoBkNwC8a8B0UoB0UoBkNsEgtBkDsd8BsTkDgZsEgtB4D0jBgFwvBoG46B8B8QwCoV8BwMgFgtBUwHkD8akDgeU4NoB4XAkIoB0ZoB8pBU0K8Boa8B0PkDkS7GkD3I0F3DwC7foa7G0Fzeoaze0ZvTiQ",
          "spans": [
            {
              "offset": 0,
              "names": [
                {
                  "value": "Invalidenstraße",
                  "language": "de"
                }
              ],
              "length": 189,
              "duration": 31,
              "speedLimit": 13.8888893
            },
            {
              "offset": 11,
              "names": [
                {
                  "value": "Invalidenstraße",
                  "language": "de"
                }
              ],
              "length": 872,
              "duration": 184,
              "speedLimit": 8.333334
            },
            {
              "offset": 44,
              "names": [
                {
                  "value": "Brunnenstraße",
                  "language": "de"
                }
              ],
              "length": 277,
              "duration": 59,
              "speedLimit": 8.333334
            }
          ],
          "transport": {
            "mode": "truck"
          }
        }
      ]
    }
  ]
}

Advertisement

Answer

Using the information in the spans object is definitely the way to go. What you need is to break up the spans into as many pieces as possible. You can do that by adding these values to the parameter in your request:

&spans=duration,length,segmentId,names,speedLimit,dynamicSpeedInfo,baseDuration,typicalDuration,segmentRef

You’ll see that the response includes a list of spans identified by the offset attribute, which tells you what coordinate in your polyline that span refers to. This means that you want to know what is the offset (coordinate index) where the sum of span durations is 137.

This procedure will get you the best approximation to the middle of the route relative to travel time:

  1. Loop through the list of spans and sum the value in the duration attribute; the loop should stop when the sum is equal or greater than the desired duration (137 in your example).
  2. Get the value of the offset attribute, and add 1.
  3. Decode your polyline, and get the coordinates at the index that is equal to the number you got in step 2 (offset + 1).

For the route in your example, the span that meets the condition in step 1 is offset=31, so you’re interested in the coordinates at index 32 from your polyline.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement