Learning about locations with Google APIs
Geocoding is the process of mapping an address or a location name to a point on Earth’s surface using latitude and longitude coordinates. Geocoding allows researchers to depict addresses on a map and perform many different kinds of analysis using geospatial data. For example, you might be interested in calculating the distance between two addresses or counting the number of particular kinds of places within a fixed radius from an address.
This post describes how to use the Geocoding API to geocode addresses. We’ll also demonstrate how to use the Places API to search around a location. For a general introduction to using web APIs for research, read our recent backgrounder.
In order to use the Google APIs mentioned above, we’ll first need to create a Google Cloud platform account and generate an API key. We can then convert a street address to (latitude, longitude)
coordinates:
Once we have the coordinates, we can start to explore the area around this address. If we want to assess how urban it is, for example, we might want to find out how many movie theaters, restaurants or banks are located within a certain radius of the address. Other studies might be interested in knowing the average distance to the nearest hospital or gas station.
The Places API offers several different facilities, or “endpoints,” for retrieving places and information about them. Because we’re interested in the types of places surrounding a certain location, we’ll use the Nearby Search endpoint. Each endpoint can take a set of parameters that customize the search in some way; for example, there are different ways to order search results. Normally, results are ranked by their prominence relative to other places in the area. Prominence can be affected by a place’s ranking in Google’s index, global popularity and other factors. If the parameter rankby
is set to distance
, places will be returned in order of distance from the location. The search radius can also be limited with the radius
parameter, in which case results are ordered by prominence within the search area. The radius
and rankby
parameters are mutually exclusive.
With just a search area defined, we will get back an unfiltered list of places of any kind. To further filter the locations returned by a nearby search, such as to just grocery stores or gas stations, we can use the keyword
parameter. Google uses these keywords to match against all content that it has indexed for the place we’re studying, so it tends to return a broad set of results.
Another way to refine our search is to filter by one of Google’s location types. For example, we can set type=bakery
to request all bakeries within the radius distance. Of course, some bakeries might not be identified as such in Google’s database and therefore would be excluded from results. For a broad keyword search such as “bakery,” we elected not to specify a type at all, instead collecting all available data and then further refining the measure by spot-checking the results.
It’s important to remember that Google returns a maximum of 60 results per search, which are returned in three “pages” of 20. The pagetoken
parameter is included in the API response if there are additional pages of results to retrieve. Our function follows these tokens and joins the pages together into one list of 60. That is important to remember if you care about getting every result from your search within a specific territory, rather than just the closest or most relevant one.
You can achieve complete coverage within a territory in two ways. If you set rankby=distance
, then you can can presume that there are no omitted results that are closer than the 60th result. Otherwise, you can constrain your search radius until the number of returned institutions is under 60 and make additional queries over the area until you have complete coverage. With both strategies, it is important to reposition the location of follow-up queries as you retrieve results. The code below, implemented in Python, returns the 60 most prominent places that are found within the specified radius of the coordinates:
Google Maps is not the only geospatial data provider. Another popular option is the Open Street Map, which crowd-sources location information. By using any of these services, researchers can better understand the geographic context of specific locations and examine whether that geography has broader implications.
This post was written by Brian Broderick, Onyi Lam and Skye Toor of Pew Research Center. Brian Broderick is a data science engineer. Onyi Lam is a computational social scientist. Skye Toor is a data science assistant.