Build your own route optimization API with Google Maps

How to use Google Maps to build your own route optimization API

Build your own route optimization API with Google Maps

Route optimization is critical for any business that needs a cost effective way to deliver goods and services to their customers. If you are looking for an easy way to integrate route planning into your own software, the fastest solution is to sign up with a route optimization API (Application Programming Interface) provider like Routific, Graphhopper or Elastic Route (new). If you are interested in building your own, read on!

Part 1: Build your own route optimization API with Google Maps (this article)
Part 2: Creating a route with the Google Maps Directions API
Part 3: Optimizing a route with the Google Maps Directions API
Part 4: Route optimization web service basics
Part 5: Let's build a route optimization web service
Part 6: Putting it all together - testing your route optimization API (coming soon)

As always, all code used in this tutorial is available under a permissive MIT license on our Github repository, and you are free to use it in part or in whole for your own projects.

What is Route Optimization?

Route optimization refers to a class of algorithms that compute the most cost effective (travel distance minimizing) way for a fleet of vehicles to complete all the jobs assigned to it. These algorithms help logistics companies answer questions such as "How many vehicles do I need to serve all my customer deliveries tomorrow?" or "For each driver, in what order should I sequence his route?".

On the web, the term route optimization is widely used to refer to two different, but closely related things.

NO: Driving directions - How to get from locations A to B in the fastest way possible.

The shortest route between two points A and B based on Google driving directions
The shortest route between two points A and B

YES: Optimal route - You need to visit locations A, B, C and D in any order, in the fastest way possible - in the example below the optimal route goes A, C, D and B. This optimization is an application of the traveling salesperson problem. With Google, travel time is the primary factor which is optimized, but other factors such as distance, number of turns and many more may be taken into account when deciding which route is the most efficient.

The shortest route through points A, B, C and D via route optimization
The shortest route through points A, B, C and D

What we are Going To Build

We are going to build a route optimization web service on top of the Google Maps Directions API . The web service essentially works like a wrapper that allows data to be sent and received in json format making it easy to humans to read and machines to parse. We'll also build some extra features like authentication (client token security) and persistence (to retrieve your data using a job id) to give you a feel for how production web services are run.

API request made to our route optimization web service
API request made to our route optimization web service

At the end of this tutorial, you are going to be able to send the API a list of visits to be served by your fleet (limited to a single driver and 25 stops - for now) and get back the same list, but in route optimized order. This list can easily be displayed on a map or printed out and given to your driver.

Next: Part 2: Creating a Route with the Google Maps Directions API