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 frequent.

An API or application programming interface, is a set of routines, protocols, and tools for building software applications.

It 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, perhaps you 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.

In fact, 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 website API’s to retrieve information.

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

To do this, create an instance of the direction finder object.

An object is a type of variable that can contain many values.

An instance is an occurrence of an object.

In this case, the object has already been defined by Google Maps.

You just need to add the values to create your instance, or to “instantiate” the object.

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

Type “V-A-R directions equals.”

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

Then, type “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 function properly!

Next, add some methods, or procedures, associated with this object.

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 using the two points.

Check your code again to be sure it matches what you see here on the screen exactly.

Great!

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 have created an instance of the object, check it out in the log.

Type “Logger dot log” and “directions.”

Then, run your program and 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 looks overwhelming right now, but in the next video, you will use this object to find the distance between your two locations.

Now, it’s your turn Create an instance of the direction finder object by creating a variable and adding methods.

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
Instructions
Create an instance of the direction finder object by creating a variable and adding methods.