Publish AI, ML & data-science insights to a global community of data professionals.

Searching places and their geographic details for geoanalytics

The Google’s Places API, in combination with the standard Python package provides an easy way to find places using names, address, phone number, keywords, or categories (restaurants, bars, stores, etc.).

Photo by Lucas Sankey on Unsplash
Photo by Lucas Sankey on Unsplash

The role of geographic data has become an essential component of political as well as business decision-making for understanding and solving specific problems. For example, I am using it for my research in sustainability, such as identifying clusters of different industries, demographics, etc., around a particular location, where firms are involved in sustainable business practices.

One of the main challenges of GIS-based research is difficulties in obtaining data. Though many standard databases exist that provide details of buildings, businesses, population, etc. in an area, the main issue with these databases is that they frequently become dated, less accurate, and unreliable. Google maps or similar services can be used to complement the existing databases with up-to-date details. In this post, I will deploy Google Places API to search places and retrieve necessary information for desirable place.

A word of caution: while the Google API services have some free quotas every month – which is more than enough for most usage, one has to provide payment details (e.g., credit or debit card) for registration before an API key can be issued. If you are unwilling to pay or fear unexpected charges, there is enough flexibility and options to restrict the requests/usage beyond the free services to prevent triggering of any undesirable billing. But, you cannot use services (to the best of my knowledge) without providing an acceptable form of payment credentials.

There are two ways to find places in Python using the official API of Google. One can directly make HTTP URL requests using the search instructions available on Google Place Search API webpage. One can also use Googlemaps package, which provides the user-friendly functionality to get place details in Python. Since using standard package is more convenient, we will use that for your exercise. Searching places in Google API can be done in four different ways.

  1. Search Nearby Places

A nearby search looks for places within an area specified by, for example, latitude, longitude, and the radius. We can refine the search request by mentioning keywords (coffee, vegetarian, etc.) or type of target places (e.g. restaurants, cafe). It searches the entire content that Google has indexed for a place. Both ways of searching (by keyword or type) often return different results. A snippet of the sample code is illustrated below:

This API request returns most details of each place, except for formatted_address, phone number, website, and reviews. Although these details may be relevant in certain cases, the nearby places functionality provides all the relevant and satisfactory information for places we are looking for.

  1. Search Places

This request is quite similar to the nearby search API discussed above, but the only difference is that it returns slightly different details. For example, the response of the query returns the addresses of the place, which is not available in nearby search function.

  1. Find a Place

This request finds a single place based on the name of the place, address, or phone number. The API request offers flexibility of specifying the parameters for which we want the data for as shown in the code below:

Making a request this way has two limitations. First, while the query based on phone number or address will certainly return one result, the text query involving the name of a place may find a different place than what we desire. So, unless we also know the exact name of the place, this function is somewhat unreliable or requires manual intervention to verify the place details. Second, the match is possibly, but not necessarily, within the target geography. When no match is found within the desirable area, it may return a closest match from anywhere around the globe rather than returning zero results – as long as any match is available in Google database.

4. Get Place Details

This is the most comprehensive Places API for finding information on a known place. The Place Details query returns all details of a place – depending on the fields argument of the request – that Google has indexed for this place, including reviews (up to 4–5 reviews for an individual account), website, etc. Similar to the find_place, this API request also offers flexibility of specifying what place attributes needs to be returned. The only difference is that it also returns a few additional details (e.g. website, reviews) that third API does not. A sample code snippet is shown below:

The main drawback of this function is that it searches using place_id and one must have the exact place_id before making a request. So, this API is often used in combination with a general search APIs, which provide place_id based on keyword search, and then use the place_id to collect place-specific information.

Potential Applications and Concluding Remarks

There are different ways people can use such APIs. For example, in my case, the API allows me to quickly retrieve the location of shopping malls, coffee shops, restaurants, bars, etc. nearby a particular office, college, or housing complex, which will help me understand the evolution of urban economies and development patterns. These commercial density and development patterns may also reveal inequities in infrastructure, businesses, shops, schools, etc. in different neighborhoods. Finding such clusters will be useful in developing policies for promoting sustainable development within a region at city or neighborhood level.

The main purpose of this blog was to introduce features of the Google Places API in Python. The Google APIs for geographic information offers a variety of benefits to app developers as well as other domain experts , such as geographic information researchers.


Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.

Write for TDS

Related Articles