Introduction to Google APIs: pt. 2 šŸŒŽ

mariamarsh

Maria šŸ¦ Marshmallow

Posted on February 6, 2023

Introduction to Google APIs: pt. 2 šŸŒŽ

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.

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
Enter fullscreen mode Exit fullscreen mode

In API Tester app it will look like this:

Image description

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

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

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
Enter fullscreen mode Exit fullscreen mode

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

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

Image description

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

Image description

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

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
Enter fullscreen mode Exit fullscreen mode

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

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

Image description

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

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

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:)

Buy Me a Coffee at ko-fi.com

You can also support me on Coinbase

šŸ’– šŸ’Ŗ šŸ™… šŸš©
mariamarsh
Maria šŸ¦ Marshmallow

Posted on February 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related