Introduction to Google APIs: pt. 2 🌎
Tags: api, googlecloud, beginners, testing
Published: 2023-02-06 12:47:59

In this post, we will continue working with Google APIs, focusing specifically on the Google Maps API. 

The Google Maps API provides a variety of endpoints that can be used to perform different types of operations. Some of the most common endpoints include:
- **The Geocoding API** (which can be used to convert addresses into coordinates);
- **The Directions API** (which can be used to calculate directions between locations); 
- **The Places API** (which allows you to search for points of interest); 
- **The Distance Matrix API** (which can be used to calculate distances between locations).

Google Maps API is a powerful tool that allows developers to add maps and location-based functionality to their applications by providing SDKs for different platforms like Android, iOS, and Javascript. With a wide range of endpoints and requests, it offers a lot of flexibility and can be used to create a variety of different types of applications.

The Google Maps API uses a RESTful architecture, which means that it can be accessed using standard HTTP requests. The API supports both GET and POST requests, with GET requests being used to retrieve information and POST requests being used to submit new data. All requests to the API must be authenticated using an API key or an access token, which can be obtained through the Google Cloud Console.

When making requests to the API, you can specify various parameters to customize the request. For example, you can use the "address" or "latlng" parameter to specify a location, and the "sensor" parameter to indicate whether the request is coming from a device with GPS capabilities. You can also use the "language" parameter to specify the language in which the response should be returned.

The Google Maps API provides several different types of responses, including JSON and XML formats. Also, the API has usage limits, it's free to use up to certain limits, but if you exceed the usage limits you will be charged. 

Below, I’d like to show a few examples of how to use Google Maps APIs with API Tester app. 

**Google Places API** 

The Google Places API is a service within the Google Maps API that allows developers to search for places, such as businesses, landmarks, and points of interest. To get access to the Google Places API, you will need to have a valid API key for the Google Maps API (check how to generate your personal API Key in [this post](https://dev.to/mariamarsh/introduction-to-google-apis-pt-1-1l5o). 

Once you have your API key, you can use it to make requests to the Google Places API using the API Tester app. You will need to add the API key to each request by including it in the request headers or as a query parameter in the request URL.

Please keep in mind that you also need to enable the specific Google Maps API you want to use, you can enable it by going to the Google Cloud Console, then navigating to the "APIs & Services" section, selecting "Library", finding the Google Maps API and enable it.

**Searching for a place** 

To search for a place using the Google Maps API and API Tester app, you can use the Places API. You would set the request method to GET, the endpoint URL for the Places API, and in the request parameters, you would specify the location, keyword, and type of place you want to search for. 

For example, for searching for a Pizza place in New York in 2000 Radius, you would use this endpoint: 

```
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.748817,-73.985428&radius=2000&keyword=pizza&key=YOUR_API_KEY
```

In API Tester app it will look like this:  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbtwpnawjor0w32rm29q.PNG)

In the query parameter section, add your API key and any other parameters that you require. Make sure to replace the example parameters with your own values. Once you have set up the request, you can test it and retrieve the desired information from the API.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20ag7cb6vur7q6l9jtdg.PNG)

Once all the information parameters and API key is correct a 200 response code will be generated and you can see it in images. I am getting all the Pizza restaurant's information, their location, timing open or closed in the 2000M area. That is what information I was required from Google Places API. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2o4y6jopduz3aa8pq0w.PNG)

**Searching for a specific place details** 

Another example of a Google Places API request is to get information about a specific place. The endpoint for this request is the following: 

```
https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJrXXKn5NZwokR78gOipCnY60&key=YOUR_API_KEY
```

To get the response, you need to add the place_id parameter and your API key. To find an ID for a specific place, you can use the request described above. In the response, you can find a unique place identifier:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ves0txwkulpnuwwyd8ry.jpg) 

Copy it and place it in the query params section of the request.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttrpwusbi1hifq67qxma.PNG)

That's basically it; now, when all parameters are set, tap the play button and explore the response:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7tmdjcsp4pc1g3kgqo3.PNG) 

As can be seen, by making an API call to the Google Places API, we received a lot of different information regarding one specific place, including address components, an overview of the place, a phone number, the name of the place, open hours, etc. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8fmwxqc5uvfqz7yg7rd.PNG) 

**Google Geocoding API** 

Another great example of using the Google API is geocoding. This function converts addresses or place IDs to latitude and longitude coordinates and vice-versa.To perform the geocoding function, use the following endpoint: 

```
https://maps.googleapis.com/maps/api/geocode/json?address=1920%20Bank%20Rue%2012%20Ottawa%20Ontario&language=en&region=en&key=YOUR_API_KEY
```

The important thing here is that you need to correctly paste the address you want to convert in the query params section. You need to:
a) Use an official postal address (the format);
b) Change all spaces between address elements to "%20."

Otherwise, you won't get the necessary response. 

> By the way, in API Tester query parameters, the description contains the rules and data for a specific parameter. This is extremely convenient because you don't need to check documentation to fill in values for query parameters.

One more important thing to take into account is that you need to state the output format (either JSON or XML) in the request URL and duplicate it in the request headers.
 
In API Tester, this geocoding request will look like this: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9utkvt3z53s76jgds3f1.PNG)

Don't forget to put your API key in the query params section! 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7e2sbit8jphi1bddryj.PNG) 
    
After all parameters are set, you can run the request. Important note: in this example, I've only used required parameters such as address and key (language and region were optional), but you can also use additional parameters such as bounds, components, place_id, and coordinates. 

For this address **1920 Bank Rue #12, Ottawa, Ontario**, we get the following response:    

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dt0avesy2fp7f2ef67w4.PNG)

As you can see, just by putting one specific address, we get information about address components, the place's name, latitude and longitude, place_id, etc.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tul524dg66a4q8wo3thk.PNG)  

**Conclusion** 

The Google Maps API and its additional services represent extremely useful data for all developers, not only in terms of exploring different information about places in the world but also by allowing developers to create their own apps or software based on this API. In this post, I've only shown three examples of using the Google Maps API, but there are many more various endpoints to use.

All in all, I hope you'll find this post helpful! Share your experience with Google Maps APIs in the comments!   

Btw, you can support my work by buying me a coffee! I'll leave here a few links for you:) 

<a href='https://ko-fi.com/K3K0H833L' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi3.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

You can also support me on [Coinbase](https://commerce.coinbase.com/checkout/d2e0aecf-b817-44fe-bbab-fbceb5741ec4)