Skip to content

5. Communicate with the API

Playback Speed:
Transcript

In this video, you will communicate with the Google Maps API to find the distance between two locations: the housing option you researched, and another location you’re likely to go to often An API, or application programming interface, allows applications to communicate with each other.

An API acts much like a waiter in a restaurant.

It is like a messenger that takes your requests, tells the system what you want to do, then delivers a response.

For example, you may have searched for a flight on an airline website.

You select from a menu of options, and the website searches the airline’s database to see if there is a flight that matches your selections.

But what if you use a travel service website instead?

That website is NOT directly connected to the airline’s database.

A travel service connects with LOTS of airline databases to find available flights.

To do this, the travel service communicates with the airline’s API to get information.

Then, it delivers the information back to you.

Lots of apps tap into API’s to retrieve information.

In the script editor, use the Google Maps API to retrieve directions between the two locations you found.

In the script editor, define a “Directions” variable.

On a new line, type "V-A-R, space, directions, space, equals."

Then type “Maps” to specify that you are working in Google Maps.

Then, type a period and “new Direction Finder.”

Make sure to type the code exactly as you see on your screen and check frequently for errors.

One small error and your code will not work properly!

Next, set the origin using “dot set Origin.”

Set the destination using “dot set destination.”

You can put these on the same line or on separate lines.

Then, get directions using the origin and destination points.

Type "dot get Directions."

End the line with a semi-colon.

Check your code again to be sure it exactly matches what you see!

Don’t worry about memorizing this code.

Most programmers don’t remember exactly how to call an API like this.

To create this code again for a different spreadsheet, you could search the internet for help and examples.

Now that you can communicate with the Google Maps API, check out the information your program can access.

Type “Logger dot log” and “directions.”

Then, save and run your program.

View the log.

Wow!

That is a lot of data!

The program pulls all kinds of information, like the coordinates of each location, directions, restricted roads….

It even tells you which side of the street your destination is on!

This all might look overwhelming right now, but in the next video, you will write code to find the distance between your two locations.

Now, it’s your turn: Use "maps dot new direction finder" to call the Google Maps API.

When your code looks like this and the script log retrieves the data from the Google Maps API, move on to the next video.

Next arrow_forward
Instructions
  1. Create an instance of the direction finder object by creating a variable and adding methods.
Attributions
  • "What is an API?" for federal government by GSA" by DigitalGov (https://www.youtube.com/watch?v=BVeiRCEwJx8) -- Licensed by CC Attribution 3.0 Unported (https://creativecommons.org/licenses/by/3.0/legalcode) -- Audio is removed from video, Clipped to fit video length needs