> ## Content Index
> Fetch the complete content index at: https://blog.afi.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# Creating a route with the Google Maps Directions API
- URL: https://blog.afi.io/blog/creating-a-route-with-the-google-maps-directions-api/
- Published: 2021-11-01T09:50:46.000Z
- Updated: 2026-08-05T07:47:44.000Z
- Description: How to create a route between two points with Google Maps.
- Author: Afian Anwar
- Tags: tech

In this tutorial, we'll use the [Google Maps Directions API](https://developers.google.com/maps/documentation/directions/get-directions?ref=blog.afi.io) to create a delivery route from a warehouse in downtown San Diego at:

 (start) 1337 India St, San Diego, CA 92101, United States

for delivery to:

(A) 1200 Third Ave, San Diego, CA 92101, United States  
(B) 707 Tenth Ave, San Diego, CA 92101, United States  
(C) 180 Broadway Suite 101, San Diego, CA 92101, United States  
(D) 945 Broadway, San Diego, CA 92101, United States

![We need to use route optimization to find a route to locations A, B, C and D that minimizes driving time](https://storage.ghost.io/c/c6/4d/c64da7e8-63a6-4cff-acdc-2782a6ebc377/content/images/2021/10/scene3.png)

We need to find a route to locations A, B, C and D that minimizes driving time

ending at (end) 1485 E St, San Diego, CA 92101, United States. What's the best route to take? Which stops should we visit first? Let's ask Google Maps.

Part 1: [Build your own route optimization API with Google Maps](https://blog.afi.io/blog/build-your-own-route-optimization-api-with-google-maps/)  
**Part 2: Creating a route with the Google Maps Directions API (this article)**  
Part 3: [Optimizing a route with the Google Maps Directions API](https://blog.afi.io/blog/optimizing-a-route-with-the-google-maps-directions-api/)  
Part 4: [Route optimization web service basics](https://blog.afi.io/blog/route-optimization-web-service-basics/)  
Part 5: [Let's build a route optimization web service](https://blog.afi.io/blog/ets-build-a-route-optimization-web-service)  
Part 6: Putting it all together - testing your route optimization API (coming soon)

### Sign up for a Google Cloud Platform Account

The first thing you need to do is sign up for a Google Cloud Platform (GCP) account, link a credit card and create a project (see [this video](https://www.youtube.com/watch?v=uINleRduCWM&ref=blog.afi.io) for instructions). Google Maps can get expensive, but the free $200 monthly credit is more than enough to cover the costs of running this tutorial.

![Google Maps standard pricing](https://storage.ghost.io/c/c6/4d/c64da7e8-63a6-4cff-acdc-2782a6ebc377/content/images/2021/10/Screen-Shot-2021-10-24-at-11.58.49-PM.png)

Google Maps standard pricing

### Find your Google Maps API Key

Next, while still logged into the GCP Console, go to the [project you just created](https://console.cloud.google.com/apis/library/directions-backend.googleapis.com?pli=1&ref=blog.afi.io) and enable the Directions API. Once that's done, go to the **Google Maps Platform > Credentials** [page](https://console.cloud.google.com/projectselector2/google/maps-apis/credentials?ref=blog.afi.io) and generate a new API key (see [this video](https://www.youtube.com/watch?v=2%5FHZObVbe-g&t=1s&ref=blog.afi.io) for a step by step walkthrough). It should look something like this:

```
GOOGLE_API_KEY: AIzaSyBpGrzvyLhnNl_BJ8e7G6LLauQ_SFsHyTI
```

You can check that everything is set up correctly by going back to the project page and making sure that there's a green tick next to the **Manage** button on the Directions API page.

![API enabled on the Directions API section of your GCP project](https://storage.ghost.io/c/c6/4d/c64da7e8-63a6-4cff-acdc-2782a6ebc377/content/images/2021/10/Screen-Shot-2021-10-26-at-11.21.54-PM.png)

API enabled on the Directions API section of your GCP project

### Make a Test Request

To make API requests, we are going to use an application called [Postman](https://www.postman.com/?ref=blog.afi.io). Postman gives us a user interface to authenticate with web services and save the results for future reference. Once you've [downloaded and installed Postman](https://www.postman.com/downloads/?utm%5Fsource=postman-home), create a new `GET` request and call this URL (remember to replace `GOOGLE_API_KEY` with your own credentials).

For this test, we will retrieve the route going from (start) 1337 India St, San Diego, CA 92101, United States to (end) 1485 E St, San Diego, CA 92101, United States but ignore stops A, B, C and D. The first thing we need to do is [encode](https://www.urlencoder.io/learn/?ref=blog.afi.io) the start and end addresses into ASCII so that it can be safely used in the API call. 

Using a [URL encoder](https://www.urlencoder.org/?ref=blog.afi.io), an address like "1337 India St, San Diego, CA 92101, United States" becomes "1485%20E%20St%2C%20San%20Diego%2C%20CA%2092101% 2C%20United%20States". 

Now, let's see how a basic call to the Directions API is structured.

```javascript
https://maps.googleapis.com/maps/api/directions/json?
origin={origin}
&destination={destination}
&key=GOOGLE_API_KEY
```

There are three parameters - **{origin}**, **{destination}** and **GOOGLE\_API\_KEY**. From the [documentation](https://developers.google.com/maps/documentation/directions/get-directions?ref=blog.afi.io), we are told that the API will return the most efficient route taken between origin and destination. Replace **{origin}** and **{destination}** with the encoded address strings for (start) and (end) and swap in your API key for **GOOGLE\_API\_KEY**. Here's what the final URL looks like: 

```javascript
https://maps.googleapis.com/maps/api/directions/json?origin=1337%20India%20St%2C%20San%20Diego%2C%20CA%2092101%2C%20United%20States&destination=1485%20E%20St%2C%20San%20Diego%2C%20CA%2092101%2C%20United%20States&key=GOOGLE_API_KEY
```

In Postman, click **File > New > Http Request** and choose `GET`. Copy and paste the URL above into Postman and hit **send**.

![api call to Google Maps Directions API](https://storage.ghost.io/c/c6/4d/c64da7e8-63a6-4cff-acdc-2782a6ebc377/content/images/2021/10/Screen_Shot_2021-10-26_at_11_59_31_PM.png)

Http GET request to the Google Maps Directions API

You'll get back a response that looks like this:

```
{
	"geocoded_waypoints": [{
			"geocoder_status": "OK",
			"place_id": "ChIJNVmHHqxU2YARKM8EALOHsNo",
			"types": [
				"street_address"
			]
		},
		{
			"geocoder_status": "OK",
			"place_id": "ChIJCwu6NF5T2YARlxk3U2DLK6g",
			"types": [
				"premise"
			]
		}
	],
	"routes": [{
		"bounds": {
			"northeast": {
				"lat": 32.7194027,
				"lng": -117.150393
			},
			"southwest": {
				"lat": 32.7147013,
				"lng": -117.167415
			}
		},
		"copyrights": "Map data ©2021 Google",
		"legs": [{
			"distance": {
				"text": "1.3 mi",
				"value": 2146
			},
			"duration": {
				"text": "7 mins",
				"value": 438
			},
			"end_address": "1485 E St, San Diego, CA 92101, USA",
			"end_location": {
				"lat": 32.7147013,
				"lng": -117.1507166
			},
			"start_address": "1337 India St, San Diego, CA 92101, USA",
			"start_location": {
				"lat": 32.7194027,
				"lng": -117.1674044
			},
			"note": {
				"comment": "...Additional results shortened in this example..."
			},
			"traffic_speed_entry": [],
			"via_waypoint": []
		}],
		"overview_polyline": {
			"points": "goufEfgcjUtBBCcJ@sDAmD@iDAiECcMCsG?gHAuFrECtCAn@?@U?k@AoA@kABqB?yAB_CAqD?mDA_C?Ib@?nCAhDAdA??`A"
		},
		"summary": "W A St",
		"warnings": [],
		"waypoint_order": []
	}],
	"status": "OK"
}
```

The most important parts of the response are the `start_address`, `start_location`, `end_address`, `end_location` and `overview_polyline`. The start and end location fields give us the GPS coordinates that our driver starts from and ends at, while the polyline shows us the exact route taken. To decode this polyline, you need to use Google's [polyline decoder library](https://developers.google.com/maps/documentation/utilities/polylineutility?ref=blog.afi.io) to convert the string "goufEfgcjUtBBCcJ@sDAmD@iDAiECcMCsG?gHAuFrECtCAn@?@U?k@AoA@kABqB?yAB\_CAqD?mDA\_C?Ib@?nCAhDAdA??\`A" into an array of latitude and longitude pairs.

![](https://storage.ghost.io/c/c6/4d/c64da7e8-63a6-4cff-acdc-2782a6ebc377/content/images/2021/10/Screen-Shot-2021-10-27-at-6.22.38-PM.png)

Encoded polyline decoded into a series of lat/lng coordinates

With this information, it's simple to join the `start_location` and `end_location` coordinates with the decoded `overview_polyline` string to display your route on a map. 

![simple route from A to B drawn on a map](https://storage.ghost.io/c/c6/4d/c64da7e8-63a6-4cff-acdc-2782a6ebc377/content/images/2021/10/Screen-Shot-2021-10-28-at-12.40.26-AM.png)

A simple route from start to end drawn on a map

Retrieving the route between our start and end points is a great first step, but for this to be useful, we need to add delivery stops to the route and make sure these stops are ordered optimally to minimize drive time. 

💡

If you need your route to take into account real time traffic, try using the Google [Routes API](https://blog.afi.io/blog/a-developers-guide-to-the-google-routes-api/) instead.

My next post shows you how to modify your API call to do this.

**This article was written by** [**Afi Labs**](https://afi.io/?ref=blog.afi.io)**, a** [**Google Maps Premier Partner**](https://www.afi.io/?ref=blog.afi.io) **and reseller. We build route optimization, navigation, and fleet tracking software on Google Maps, and offer volume pricing on GMP licensing.** [**Talk to an engineer**](https://afi.iohttps//www.afi.io/contact%5Fus?ref=blog.afi.io) **or** [**follow Afian on LinkedIn**](https://www.linkedin.com/in/afian-anwar/?ref=blog.afi.io)**.**

Next: [Part 3: Optimizing a Route with the Google Maps Directions API](https://blog.afi.io/blog/optimizing-a-route-with-the-google-maps-directions-api/)