This video is available to students only

Get Started With Google Geocoding API for Location Data

We've managed to create the capability for users to query for a list of listings that can be sorted based on listing price. In this module, we'll focus on the functionality that will allow our users to search for listings based on location. We'll begin the investigation for this by discussing the API we intend to use to help us - Google's Geocoding API.

Google's Geocoding API#

📝 A sample of the .env file in our server project, after this lesson is complete, can be found - here.

We have a collection of listings stored persistently in our database. We've created the /listing/:id route in our client to allow users to view information about one certain listing at a time. We've also managed to create the capability for users to query for a list of listings that can be sorted based on listing price.

In this module, we'll focus on the functionality that will allow our users to search for listings based on location.

There are probably many ways to accomplish this. A very simple approach could be that we allow users to fill out a form and let our users provide values for a city, state, and country. In our database, we can try and have our listing documents contain some of this data and we can filter and find the correct listings based on user searches. This is a more a self-developed approach and there's a lot of questions to think about here - how do we handle spelling mistakes from users providing location information? How do we handle accepting information for many different regions in the world where certain regions may not have the concept of a state, province, etc?

We personally would want something a little more robust, like how Airbnb handles things. In the Airbnb website, there often exists an input field where the user can type a city, a state, a country or a combination of those, and the server will find listings that best matches the user's input.

Note: Airbnb does provide searching capabilities in many different ways. We've just highlighted the more straightforward approach where a user can search for any location and results are shown based on that location.

Lucky for us, there is a perfect API that can help us achieve something like this. This where we'll use Google's Geocoding API.

Google Geocoding API#

Google's Geocoding API is a service that provides geocoding and reverse geocoding of addresses. From the Google Geocoding API documentation:

  • Geocoding is the process of converting addresses (like a street address) into geographic coordinates (like latitude and longitude), which you can use to place markers on a map or position the map.

  • Reverse geocoding is the process of converting geographic coordinates into a human-readable address.

We're going to spend more time in the next lesson talking about how we'll want to use the API and what our proposed solution will be but we'll be attempting to geocode location information. A user is to provide an address and Google's servers will respond with more information about the address including its latitude and longitude.

Here's an example geocoding response, for the address input of "1600 Amphitheatre Parkway, Mountain View, CA", that Google shows us in their documentation.

In the geocoding response, there's an address_components array within the result that contains specific information about the location's street_number, route (Amphitheatre Parkway), locality (which refers to the city or political entity) among other information that can't be derived from reading the original address input such as administrative_area_level_2, postal_code, etc.).

Administrative area is the term used to convey the entities below the country level. Within the context of a country like the United States, administrative_area_level_1 often refers to the state and administrative_area_level_2 often refers to the county. The Address Types and Address Component Types section of the Google Geocoding API documentation explains this some more.

We can also see more information being returned from the sample geocoding response above such as a formatted_address field, latitude and longitude of the location, etc. For our use case and how we intend on geocoding locations for our app, we'll primarily focus on the address_components section of the Geocoding API response. When a user searches for a location, we'll be interested in three things in particular:

Start a new discussion. All notification go to the author.