A driving distance API calculates the road distance and estimated travel time between locations using a road network rather than straight-line distance.
Depending on your use case, there are two common approaches:
Routing API — best for calculating the driving distance between two locations. It returns the route, distance, and travel time.
Route Matrix API — best for calculating driving distances between multiple origins and destinations. It returns a matrix of distances and travel times in a single request.
Common use cases include delivery applications, logistics platforms, fleet management systems, store locators, and travel planning tools.
In this guide, you'll learn how to use both the Geoapify Routing API and the Geoapify Route Matrix API, understand when to choose each one, and implement practical examples for common use cases.
Table of Contents
- What Is a Driving Distance API?
- Simple Point-to-Point Requests
- Multiple Origins and Destinations
- What Should You Choose?
- Conclusion
- FAQ
What Is a Driving Distance API?
A driving distance API is a web service that calculates the distance and travel time between locations by following the road network. Unlike straight-line distance, it returns both the driving distance and estimated travel time.
Most driving distance APIs can return:
- Driving distance (for example, in kilometers or miles)
- Estimated travel time
- The route geometry (the path to display on a map)
- Turn-by-turn navigation instructions (depending on the API)
There are several types of APIs that can calculate driving distance, each designed for a different use case:
- Routing API – Calculates the route, driving distance, and travel time between two or more locations in a specified order.
- Route Matrix API – Calculates driving distances and travel times between multiple origins and destinations in a single request.
The type of API you should use depends on the number of locations involved. Let’s look at the two most common approaches.
Simple Point-to-Point Requests
If you need to calculate the driving distance between two locations, a Routing API is the best choice. It calculates the optimal route along the road network and returns the driving distance, estimated travel time, and, if needed, the route geometry for display on a map.
Let's take a look at how to calculate the driving distance between two locations using the Geoapify Routing API.
Request
To calculate a route, send the origin and destination coordinates as waypoints and specify the travel mode. The API returns the optimal route together with the driving distance, estimated travel time, and route geometry.
GET https://api.geoapify.com/v1/routing?waypoints=33.853389239358165,-79.04971896783161|33.824589565004985,-79.08103360403982&mode=drive&apiKey=YOUR_API_KEY
Result
Here is the result route:
The response includes detailed routing information. For calculating driving distance, the most important fields are:
-
distance– driving distance in meters -
time– estimated travel time in seconds -
geometry– the route geometry for displaying the route on a map
{"features":[{"properties":{"distance":6080,"distance_units":"meters","time":448.307},"geometry":{"type":"MultiLineString","coordinates":[...]}}]}
In this example, the driving distance is 6,080 meters (6.08 km) and the estimated travel time is approximately 7 minutes and 28 seconds.
Interactive Demo
You can also test simple point-to-point routing directly in the Geoapify Routing API Playground.
👉 Routing API Playground: https://apidocs.geoapify.com/playground/routing/
Use it to change waypoints, try different travel modes, and inspect the route distance, travel time, geometry, and instructions returned by the API.
Multiple Origins and Destinations
When you need to calculate driving distances between many locations, sending separate routing requests for every pair quickly becomes inefficient.
A Route Matrix API solves this by calculating distances and travel times between multiple origins and destinations in a single request.
Let's take a look at how this works using the Geoapify Route Matrix API.
Request
POST https://api.geoapify.com/v1/routematrix?apiKey=YOUR_API_KEY
{"mode":"drive","units":"metric","sources":[{"location":[-118.2365,34.05622]},{"location":[-118.49747,34.00935]},{"location":[-117.91874,33.80326]},{"location":[-117.985774,33.885559]},{"location":[-118.255841,33.806433]}],"targets":[{"location":[-117.16184,32.70633]},{"location":[-116.53897,33.82301]},{"location":[-117.19084,32.998364]},{"location":[-117.032212,32.865225]},{"location":[-116.961336,32.726207]}]}
Result
Here is the result Route Matrix:
The response contains a sources_to_targets matrix. Each row represents one source, and each item in that row represents the driving distance and travel time to one target.
{"sources_to_targets":[[{"distance":198174,"time":7118,"source_index":0,"target_index":0},{"distance":173200,"time":6259,"source_index":0,"target_index":1}]],"units":"metric","distance_units":"meters","mode":"drive"}
For example, the first result means:
-
source_index: 0→ the first source location -
target_index: 0→ the first target location -
distance: 198174→ 198.174 km -
time: 7118→ about 1 hour 59 minutes
With 5 sources and 5 targets, this request returns 25 driving distance calculations in one API call.
Interactive Demo
If you'd like to see the Route Matrix API in action, check out this interactive CodePen example. It demonstrates how to calculate driving distances and travel times between multiple origins and destinations and visualize the results.
👉 CodePen: https://codepen.io/geoapify/pen/jEyZmOg
Feel free to fork the project, experiment with different locations, and adapt the code for your own application.
What Should You Choose?
Choose the API based on your use case:
| If you need to... | Use... |
|---|---|
| Calculate the driving distance between two locations | Routing API |
| Calculate the driving distance for a route with several stops in a fixed order | Routing API |
| Display the route on a map | Routing API |
| Calculate driving distances between multiple origins and destinations | Route Matrix API |
| Compare travel times or distances between many location pairs | Route Matrix API |
| Find the nearest destination from a list | Route Matrix API |
In general, use a Routing API when you need detailed information about one route, and a Route Matrix API when you need to compare many routes efficiently.
Conclusion
Calculating driving distance with an API is straightforward once you choose the right approach.
- Use the Geoapify Routing API when you need the driving distance, travel time, and route between two locations or along a predefined route.
- Use the Geoapify Route Matrix API when you need to compare driving distances between multiple origins and destinations efficiently.
If you also need to determine the most efficient order of stops, consider using the Geoapify Route Optimization API, which automatically finds the optimal sequence of destinations.
Ready to build your own application? Explore the Geoapify API documentation for implementation guides, interactive playgrounds, and additional routing features.
FAQ
What is a driving distance API?
A driving distance API calculates the road distance and estimated travel time between locations by following the road network instead of measuring the straight-line distance.
How is driving distance different from straight-line distance?
Straight-line distance measures the direct distance between two points, while driving distance follows the available roads, intersections, and routing rules, making it the distance a vehicle can actually travel.
What is the difference between a Routing API and a Route Matrix API?
A Routing API calculates a single route and returns the driving distance, travel time, and route geometry. A Route Matrix API calculates driving distances and travel times between multiple origins and destinations in a single request.
When should I use a Routing API?
Use a Routing API when you need the driving distance, travel time, or route between two locations or along a predefined sequence of stops.
When should I use a Route Matrix API?
Use a Route Matrix API when you need to compare driving distances or travel times between multiple origins and destinations, such as warehouses, stores, or delivery locations.
Can I calculate driving distance between multiple locations?
Yes. A Route Matrix API is specifically designed for this use case and efficiently returns driving distances and travel times for multiple origin–destination pairs.
Does a driving distance API return travel time?
Yes. Most driving distance APIs return both the driving distance and the estimated travel time. Many also provide route geometry and turn-by-turn navigation instructions.
Can I display the calculated route on a map?
Yes. Routing APIs typically return route geometry that can be displayed on interactive maps using libraries such as Leaflet, MapLibre GL, or Google Maps.
Which travel modes are supported?
Most routing services support multiple travel modes, including driving, walking, cycling, and sometimes trucks or motorcycles. The available modes depend on the API provider.
What is the best driving distance API?
The best API depends on your requirements. If you need to calculate a single route, a Routing API is the best choice. If you need to compare driving distances between many locations, a Route Matrix API provides a faster and more efficient solution.