Google Maps API Implementation and Mapping Solution (Part 1)

Google Maps Platform Introduction

In today’s modern landscape, all aspects of human life are becoming technology oriented. From our shopping habits to our living styles and even our transportation, everything has moved towards a more digital approach. Gone are the days where people carried paper maps or stopped at every turn to ask for directions. Nowadays, there are multiple applications available like Google Maps, Bing Maps, Apple Maps, or Waze, which specialize in certain countries and regions. These apps have become the norm for travel guidance as they navigate the user independently and accurately. With Google being the giant in terms of worldwide navigational services, their Google Maps Platform is widely preferred in the developer community. Their vast range of Web Service APIs like the Google Distance Matrix API, Google Places API, Google Directions API, etc. provide application developers with ample functionality to play with. 

The use of these apps includes the user entering their starting location, destination, and any waypoints that might come in between.  All these apps then provide them with the best paths for their selected journeys along with detailed information about each route, including individual ETAs, estimated distance, directions, etc. Technology like this has certainly revolutionized travel and has become a notable example of how tech can be used to save time. Moreover, the best part for us developers is that our use of these facilities is not limited to these applications. Companies like Microsoft, Google, and Mapbox have made a substantial portion of the services in their apps available to the public to use in the form of Web APIs.  

In this article, we will be focusing on one of the Google Maps Platform Service APIs titled Google Distance Matrix API.  We will discuss its usage, what sort of data it returns and how users can embed it in their projects and applications. 

Google Maps Platform Setup 

They might have made their services public, but these companies certainly aren’t giving them out for free. To avail any service from the Google Maps Platform, follow the instructions for setting up the Cloud Console and API Keys. These instructions will help you set up your project on the Google Cloud Console and your API keys for usage and billing purposes.

Google Distance Matrix API 

Google’s Distance Matrix API is used to estimate the distance and time between two locations based on their coordinates. The Distance Matrix API has the following format:

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins= 33.1198628,-117.2827128&destinations= 32.8270563,-117.3100302&key=XXXXXXXXXXXXXX 

The URL includes a few optional and required parameters needed to get the required result from the API. The below image shows the list of required parameters:. 

parameters

Google Distance Matrix API Required Parameters 

The origins parameter is the starting points of a journey, and the destination would be the ending point. You would get the API key from the Google Cloud Console after setting up your project. All parameters are added after the main URL, followed by the output format, which could be either JSON’ or ‘XML.’ 

https://maps.googleapis.com/maps/api/distancematrix/outputFormat?parameters  

The parameters would be in a specific format; the parameter’s name followed by a value, separated by ‘=,’ and different parameters would be separated by an ampersand sign ‘&.’ 

origins= 33.1198628,-117.2827128&destinations= 32.8270563,-117.3100302 

The output result could be in either JSON or XML format. In this and future articles related to Google Maps Platform, we will be focusing on output results in the JSON format. The following image shows the output results in JSON for a basic GET request. 

JSON or XML format.

The first few keys are lists of the addresses of both destination and origin locations in string format. The last ‘status’ key contains a string that is used to represent whether the response was successfully retrieved or not. However, the most important set of key-value pairs are against the ‘rows’ key. The ‘rows’ key contains an array of maps in which each map has an array of maps inside it denoted by the ‘elements’ key. Here the API returns a list of objects containing the estimated time and distance of each destination coordinates provided against each origin location.

Google Distance Matrix API Multiple Destinations and Origins 

In case multiple origins and destinations are given, the API computes the values as is done in sets in mathematics, i.e., the API would return an array of  JSON objects where the first index would contain values against the first origin location against each destination and so on.

image005

Multiple origins and destination addresses could be added by placing a vertical bar ‘|’ between them, as shown below:.  

origins= 33.1198628,-117.2827128|32.8270563,-117.3100302 

The JSON objects inside the ‘elements’ key contain estimated distance values and times between specific origin and destination coordinates. The estimated distances are inside the ‘distance’ key, and time would be inside the ‘duration’ key. Both keys contain a map having two fields each: ‘text’ and ‘value’ containing a string with data in the representable form and the actual approximate value of either the distance or time, with the value being in seconds if the duration and in meters if the distance (in case of metrics selected as the unit type, discussed below). These JSON objects could be parsed in your applications to attain these values and be converted into any format you desire.

Google Distance Matrix API Optional Parameters 

Apart from the required parameters we discussed above, the Distance Matrix API gives us a wide range of optional parameters to add to our request to modify the response we need accordingly. For example, we could add the ‘units’ parameter to specify whether we need the value in imperial or metric format. We could specify the travel mode for which we require our estimated values, i.e., driving, walking, etc.  

units=metric&mode=transit  

The following endpoint contains parameters that could be used to estimate the time and distance between two locations for a transit route by bus and optimized to include the least walking.  

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins= 33.1198628,-117.2827128&destinations= 32.8270563,-117.3100302&mode=transit&transit_mode=bus&transit_routing_preference=less_walking&key=XXXXXXXXXXXXXX 

The next API endpoint would return values for routes between two origin points and two destination points where the travel mode would be driving, with setting the departure time to now, the ‘traffic_model’ parameter set to optimistic. The values returned would be considering the best-case scenario in terms of traffic and asking the API to choose a path where tolls would be avoided.  

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric& origins=33.1198628,-117.2827128|32.8270563,-117.3100302|&destinations=32.8270563,-117.3100302|33.1198628,-117.2827128|&mode=driving&departure_time=now&traffic_model=optimistic&avoid=tolls&key=XXXXXXXXXXXXXX 

Suppose we specify either the ‘departure_time’ parameter or the ‘traffic_model’ parameter. In that case, the API will return another value in response inside the ‘elements’ JSON object with the key ‘duration_in_traffic.’ 

You probably have guessed what sort of value this field might contain the total amount of traffic time the chosen route might entail.

traffic time

It is important to note that the API would select the best route for individual coordinates, and the values returned would be following that one single route selected by the API.  

For a more in-depth overview of the Google Distance Matrix API, visit the official documents here: 

 https://developers.google.com/maps/documentation/distance-matrix/overview

Conclusion 

This article was just the first part of exploring the Google Maps Platform. These APIs are used for multiple use cases in apps that require navigation or travel-based uses. Considering these are publicly available web APIs, these could be used in any sort of application independent of the platform. 

In the future, we will explore more Web Service APIs available in the Google Maps Platform, and by using these how we can customize our version of the Google Maps Experience. Stay tuned!