Skip to main content

Calculate a Car Route

Problem: I need a driving route between two points, with a duration I can show a user.

Prerequisites

  • A HERE API key with Routing entitlement
  • export HERE_API_KEY="..."

The code

Response walkthrough

duration is seconds. length is metres. polyline is HERE’s flexible polyline encoding — decode it with HERE’s published algorithm, not Google’s. When no route exists:
HTTP status is 200. Checking resp.ok swallows this entirely. Always check routes.length and read notice.The classic trigger is an ocean between origin and destination. In truck routing, it means no legal path exists — which is correct, and must not be retried with relaxed constraints.

Common mistakes

Reversing coordinate order. HERE takes lat,lng. GeoJSON is lng,lat. This bites once, and it is always at 2am. Treating 200 as success. Omitting return. You get a default shape, which may change. Be explicit. Requesting instructions for a backend ETA. Nothing consumes them. Letting departureTime default when the departure is in the future. A route computed at 2pm for a 6pm departure inherits 2pm traffic. Unencoded waypoint attributes. Values like nameHint may contain & and !, which collide with URL control characters. Retrying 403. Permanently futile. Prototyping on car and shipping truck. Different engine, different results. See Truck Route.

Production considerations

Cache geometry and ETAs separately. The path between two fixed points is stable for hours. The duration is not. One TTL for both serves stale arrival times to drivers. Do not re-route on every GPS ping to refresh an ETA. Recompute the remaining duration from the polyline and current position — arithmetic on data you already hold. Call routing when the driver deviates. See ETA Calculation. If you are calling this in a loop, stop. You are building a cost table and want Distance Matrix. A 20×500 problem is 10,000 routing calls or one matrix call. Set a timeout. Routes with many waypoints and traffic enabled are not instantaneous. Some transport modes are beta. bicycle, bus, and privateBus carry beta status with limited functionality. Verify before shipping.

Routing

Transport modes, return fields, and error semantics.

Routing vs Matrix

The loop that costs orders of magnitude.

Truck Route

Same endpoint. Entirely different correctness requirements.

ETA Calculation

Why a routing duration is not a delivery promise.

HERE documentation


Need production HERE API keys or implementation support? Placematic is an official HERE Technologies reseller and implementation partner. Talk to us.