How to Use the Google Maps Places API in R
Unlock point of interest data using 2 lines of code in R
When I learned about the Google Maps Places API, I was excited by the possibilities that the API unlocks, but left confused as a non-software developer trying to decipher the documentation. So, I wrote up an article to give a step-by-step guide on how to use it in Python for others facing the same challenge.
I mentioned in the article that the code could easily be transitioned to R, but left it at that. I’ve found myself using R recently, and it turns out the implementation of the API in R is even easier than Python.
What is the Google Maps Places API?
Simply put, the Google Maps Places API is a way to access information you’d find when searching Google Maps. If I want to find Mexican food in downtown Raleigh, NC, I’d search on Google Maps and see what pops up. The Places API is a way to programmatically source the results that pop up.

A point to note, though, is that the results will not return every result in view. Consider how the results on your screen change depending on the center point of your map and zoom radius.


Google provides a sampling of the results depending on its relevance, location, paid advertisements, etc. It’s easy to think that you can just input a wide search radius and get all results, but no such luck. Plus, each search is limited to a maximum of 60 results.
So, be targeted and specific with your searches when using the API.
How to Use the Google Maps Places API
The googleway R package dramatically simplifies the process of accessing the Places (and other Google Maps) API’s, in addition to preparing the data in an easy-to-use format.
Install the package just as you would any other with install.packages('googleway') and load the package with library(googleway).
To use the package — and any Google API — you’ll need to set up a Google Cloud account and generate an API key. Here are instructions on how to do that. You’ll get $200 in free credits per month for Maps-related API billing, which is the equivalent of about 11,750 searches, so you’d have to work pretty hard to exceed the free limit.
When you have your API key, you’re ready to access the point of interest data in as little as 2 lines of code:
Line 1: Use the google_places function to make a call to the API and save the results
Line 2: Access the search results
To work most effectively, the google_places function takes a search string, location, radius, and your API key.
google_places(search_string = 'mexican food', location=c(35.77936902425637, -78.63995745574967), radius=3218, key=gmaps_key)- The
search_stringis simply the search we want to execute. - The
locationis the latitude and longitude of the center point for our search (here, downtown Raleigh) inputted as a vector. I usually just right click on the place I want to search in Google Maps and it will generate the coordinates as the first menu result. - The
radiusis the distance in meters that we want to search. Here, I converted 2 miles to meters for about 3,218 meters - The
keyis the API key. I’ve saved it as a separate variables,gmaps_key, but you could input it directly (in quotes) if you were only doing one search.
When you run the code, you’ll see two boxes pop up in R Studio. First is a “response” page and second are the results.
The response page provides you with the next_page_token (more on that later), the status of your request (“OK” means good to go, otherwise problems), and your results.

To access the data, either click into the data frame as the second popup, or — assuming you’ve saved your search — access it by running variable_name$results. I’ve saved my results to the variable search_str so I call the results with search_str$results.

The data frame will give you a wealth of information about each place, including its address, latitude & longitude coordinates, price level, star rating, number of ratings, categories, and more. Some of these are readily available, and some of them you’ll need to write a bit more code to access.
To get the latitude and longitude coordinates, for example, you need to access the data frame within each value in the geometry row above, then within location. Use the code variable_name$results$geometry$location to access the coordinates.

If you want to bind them back to your original data frame, just run the code:
variable_name$results %>% cbind(., variable_name$results$geometry$location)
This will add the latitude and longitude as columns to your search results. Likewise, if you want to access the types, use the dollar sign notation for the types column to access that information.
You may have noticed there were only 20 results even though I said it returns up to 60. This could mean there were only 20 total results, but usually it means something else.
Each API call returns up to 20 results, and a search can return up to 60 total places. This effectively means that to access all 60 results, you need to execute 3 calls to the API. The next_page_token that I mentioned above is how we’ll access the next (up to) 20 results.
To do this, run another search using the same search criteria, but this time add one more parameter to the function, the page_token.
google_places(search_string = 'mexican food', location=c(35.77936902425637, -78.63995745574967), radius=3218, key=gmaps_key, page_token = search_str$next_page_token)The page_token is the way to tell Google to return the next 20 results in the search instead of the first 20. If you’ve saved your initial search, you can add the next_page_token directly by calling variable_name$next_page_token.
If you want to return the last 20 results (places 40–60), you can access the next_page_token from your second search and insert it into the page_token parameter.
If there’s no next_page_token, then there either (1) weren’t more results than are already showing or (2) you’ve already maxed out your 60 results.
Other Things to Know
It’ll take a couple searches to get the hang of how the API returns results, but with $200 in free credits, I encourage you to test and learn early on.
Smaller search radiuses will give you more confidence that all your desired results are showing up, but if you’re trying to look in a wider region, you’ll have to execute more searches. It’ll take some iteration to find the trade off that’s right for your use case. (Run a couple test searches on Google Maps if this point is confusing and see how the results change based on your zoom level. The API acts the same way — some results will appear, others won’t, but at more zoomed in levels, your precision will improve.)
Be sure to understand how the Terms of Service affect your use case, particularly around licensing and use of the data.
The googleway R package has significantly more functionality than just the google_places function. If you want to geocode, find distances between points, get directions, or more, this is your place to go.
I can’t emphasize enough how much easier the developer made the process of accessing the Google Places APIs, and with easier access unlocks more interesting use cases and applications.
Want to talk more location analytics? Connect with me on LinkedIn.






