Learn how to extract valuable business data from Google Maps using JavaScript and the Google Maps API.
This comprehensive tutorial covers geocoding, business search, and directions with practical code examples.
Table of Contents
- Introduction to Google Maps Data Extraction
- Why Scrape Google Maps?
- Setting Up Your Google Maps API
- Example 1: Geocoding an Address
- Example 2: Searching for Businesses
- Example 3: Creating Directions
- Legal Considerations and Best Practices
- The Limitations and Alternative Solution
- Frequently Asked Questions
- Conclusion
Introduction to Google Maps Data Extraction
Google Maps is a huge database indeed. Every time you perform a search, you get a list of companies, and each business contains commercial information and segmentation data such as reviews, opening hours, or location and contact data such as telephone number or website.
To gather all of these, scripts are created to automate large-scale extraction. This process is known as web scraping or Google Maps data extraction. For a more comprehensive overview of this process, you can also check out our complete guide to Google Maps scraping.
Why Scrape Google Maps?
Scraping Google Maps - yes, but for what purpose? Usually it's for prospecting, but not always. More specifically, you can find leads with better targeting - targeting for filters, locations, or categories. You can also monitor the competition, get an idea of prices charged, facilities in a place, or even the dishes listed on a menu. Finally, you can locate a new catchment area based on businesses nearby.
In all cases, one might wonder: how do I script Google Maps? To do this, we use the Google Maps API with JavaScript. In this comprehensive tutorial, we will cover the basics of the APIs with three practical examples: geocoding an address, searching for businesses, and displaying an itinerary.
Setting Up Your Google Maps API
Before making use of the Google Maps API, we have to create an API key through the Google Maps developer console. So I go to console.cloud.google.com and I'm going to create a new project. I select a project, I create a new project, I give to my project a name and I click on create. It will take a few seconds and I select my project. My project is selected.
Each time I'm going to use an API, I will have to activate it beforehand. In that case, I go to APIs and Services library, and if I want to use the geocoding API, for example - we will use it in a few minutes - I click on geocoding API and I enable it.
Once it is done, I will automatically have access to my API key. If it's not done already, you will also have to create a billing account. It's due to the fact that the Google Maps API is a paid API. And if I need to take a look at my API key one more time, I click on keys and credentials, and here it is.
For a more detailed walkthrough of this process, you can refer to our step-by-step guide to getting your Google Maps API key.
Example 1: Geocoding an Address
To geocode an address, we are going to use the geocoding API. Its purpose is the following: based on a place ID or a formatted address, we will be able to retrieve coordinates, meaning the latitude and the longitude.
So to write the code using JavaScript, it might look something like this. Unfortunately, I wasn't able to make it work - it seems like developer is not my job! However, I know a little bit of code, and with JSON requests, we can end up with a similar result.
Actually, the process is pretty straightforward. Here is the URL - we just have to replace the output format by JSON or XML. In our case, we will use JSON and the parameters by some parameters. The first one is places ID or address, and the second one is our API key. Pretty straightforward.
The Code Implementation
So the first thing I'm going to do is to import JSON and to import requests. Let's make our request: request.get
. I insert the beginning of my URL. I can simply copy and paste it. I replace output format by JSON and the parameters.
So the first one is the address, and the address I'm going to implement is the following. Unfortunately, I cannot directly insert it as it is because it's not an encoded address. As you can see, we have white spaces, we have commas, and all of these are kind of special characters, so we'll have to reformat this address.
To do it, I import URL lib.parse. I create a variable address which is my address, and I create another variable which is named encoded address, and it is equal to URL lib.parse.quote address. And if we print encoded address to see what the result looks like, we have an encoded address, which means an address we can insert within a URL.
Let's come back to our requests. I can create an F string: address which is equal to encoded address, and I end up by inserting my key: and key equal my key. There we go!
Let's see if we've got something. It seems our request is valid. I can type r.text
to see what the result kind of looks like, but we have imported JSON - it's to make use of it. So I create a data variable which is equal to r.json()
, and we have something which is a JSON.
If I want to extract the latitude and the longitude, it is located just here. So how to reach both of them? Let's start by the latitude. The latitude is located within results - the data results zero, then geometry, then location, and lat. I copy and paste the code to find the longitude: lng, lng. And here is how I can extract google maps data using geocoding.
For more advanced coordinate extraction techniques, check out our comprehensive guide to scraping Google Maps coordinates.
Example 2: Searching for Businesses
For the second example, we are going to scrape leads based on a category and the location, and we can do that thanks to the Places API, or to be much more specific, thanks to the nearby search API. Before using it, please make sure that you have enabled the API.
And as usual, if you want to make use of JavaScript, you can find the code in the description. But same thing as the first example - even though we are not developers, we can perfectly do that. Everything is clearly written in the documentation. We're going to copy and paste the code and we'll reformat it afterwards.
As an example, we are going to script 10 restaurants in New York. And what we are going to do is to create two JSONs - the first one with these data and the other one with the headers part. We will call this JSON payload, for example, which is equal to all of this.
Setting Up the Business Search
We want to scrape restaurants. We want 10 restaurants as a number - I can write something from 1 to 20. And here is the location restriction. The location is a circle with a radius of 500 meters. It can be from range 1 to 50,000.
Maybe we are going to change the latitude and the longitude. To do that, we can simply make a Google Maps search. I click on a point and I've got the coordinates - the latitude and the longitude. Here we go.
Now I create another JSON file. It will be named headers, and I insert the content type. Content type, I put a comma, then I insert my API key. Here is my key, comma, and the last thing we're going to add is the field mask.
Understanding Field Masks
What is a field mask? Field mask is the set of data fields we are going to extract. Here we will make things simple - we are only going to extract companies' names and formatted addresses. So Google field mask... to find the related field mask, I have to take a look at the documentation. I scroll down a little bit, and here they are: places.displayName. I put a comma followed by my second data field, which is places.formattedAddress.
I think everything is set up now. I can make use of request, but this time we are not going to make a request.get
- we're going to make a request.post
. Post, the URL is the following, and the first parameter I insert is JSON which is equal to payload, and the second one is headers which is equal to headers.
I've made a mistake - it's because I have to insert this within quotes. Here we go. And if I type r.json()
, I've got one, two, three... eight, nine, ten elements.
If you're specifically interested in extracting email addresses from these business listings, our guide on how to find email addresses from Google Maps provides detailed strategies for lead generation.
Example 3: Creating Directions
The last example I'm going to show you will enable us to create kind of directions on a map. As usual, you can find the JavaScript code in the description, but in my case, I'm going to do something slightly different by using the Directions API.
If we take a look, the request URL looks something like this, which sounds very similar to the first example we have done: output format JSON or XML, and parameters. And actually, we have a lot of parameters.
Understanding the Directions API
So Directions API - what is its purpose? Its purpose is to determine the travel time or the directions from a point A to a point B. So as parameters, we are going to insert the origin, the destination, the mode - meaning walking, driving, bicycling, or transit - the API key, and the departure time.
All right, I'm going to create my five variables. Origin, which is equal to place ID, because for the origin and destination, we can input an address, coordinates, or places ID. So to find out these two IDs, I have made use of the geocoding API: place ID of Montreal, and as a destination, place ID of Ottawa.
The mode - actually, I don't have to insert the mode because by default it will be equal to driving. The key, and the departure time. In that case, we are not allowed to insert a date - we have to insert a timestamp, meaning the number of seconds since the 1st January 1970.
We can write a little bit of code to convert a date into a timestamp, or we can make things simpler and go to epochconverter.com. We type a date and we click on "your date to timestamp." I've got my timestamp now.
Making the Directions Request
Let's make use of requests. I copy and paste my URL. I change my output format to JSON and I insert my parameters one by one. Let's create an F string: departure_time is equal departure_time, and origin is equal to origin, and destination is equal to destination, and mode is equal to mode, and key is equal to key.
Very well! r.json()
- so here is something, and it seems like we can drive from Montreal to Ottawa in 2 hours. Very versatile!
Legal Considerations and Best Practices
Before you start extracting data from Google Maps, it's important to understand the legal landscape. Is it legal to scrape data from Google Maps? The answer depends on how you do it and what data you're accessing. When using the official Google Maps API, you're working within Google's terms of service, which generally allows for legitimate business use of publicly available data.
However, aggressive scraping that violates rate limits or attempts to access private data can lead to legal issues. Always ensure you're:
- Respecting rate limits and quotas - The Google Maps API has specific usage limits
- Using official APIs when possible - This ensures compliance with terms of service
- Only accessing publicly available information - Don't attempt to access private business data
- Following Google's terms of service - Stay updated with the latest API usage policies
Additionally, be aware that there are scraping limits. What is the limit of scraping Google Maps? Most APIs have daily quotas, and direct scraping may be limited to around 120 results per request to prevent abuse.
For a detailed analysis of the legal aspects, we recommend reading our comprehensive article: Is it allowed to scrape Google Maps?
The Limitations and Alternative Solution
Using the Google Maps API does have some limitations in terms of daily quota and error handling. Each API call has associated costs, and the Google Maps API key cost can add up quickly when extracting large amounts of data. Additionally, there are specific rate limits and usage restrictions that must be respected.
To fill these gaps, we have created Scrap.io.
Scrap.io is a perfect solution for scraping Google Maps without API limitations. You can get your CSV or Excel file of leads in just a few clicks. You just insert a category, a location, and retrieve the corresponding data. This approach allows you to extract data from Google Maps to Excel format effortlessly.
With Scrap.io, you can:
- Extract business listings from any location worldwide
- Get contact information including emails and phone numbers
- Export data to CSV or Excel formats
- Access over 200 million business listings
- Process up to 5,000 queries per minute
For those who prefer browser-based solutions, you might also want to explore our guide to the best Chrome extensions for Google Maps scraping.
Frequently Asked Questions
Is it possible to extract data from Google Maps?
Yes, it is absolutely possible to extract data from Google Maps. You can use the official Google Maps API for legitimate data extraction, or use specialized tools like Scrap.io for more comprehensive data extraction without coding requirements.
Is it legal to scrape data from Google Maps?
Scraping Google Maps data is legal when done properly. Using the official Google Maps APIs ensures compliance with Google's terms of service. The legality depends on:
- Using official APIs and respecting rate limits
- Accessing only publicly available information
- Following applicable terms of service
- Not violating copyright laws
Can you export data from Google My Maps?
Yes, you can export data from Google My Maps. Google provides built-in export functionality that allows you to download your maps in KML/KMZ format through the My Maps interface.
How to scrape data from Google Maps?
There are several methods to scrape Google Maps data:
- Using Google Maps APIs (recommended) - Official method with proper authentication
- Browser automation tools - Using tools like Selenium or Puppeteer
- Specialized scraping tools - Like Scrap.io for no-code solutions
- Manual extraction - Using browser extensions for small-scale extraction
What is the limit of scraping Google Maps?
Google Maps scraping limits vary depending on the method:
- Official APIs: Have daily quotas and rate limits (varies by API type)
- Direct scraping: Often limited to ~120 results per search query
- Commercial tools: May have different limitations based on their infrastructure
How much does Google Maps API cost?
Google Maps API pricing varies by service:
- Geocoding API: ~$5 per 1,000 requests
- Places API: ~$32 per 1,000 requests for basic data
- Directions API: ~$5 per 1,000 requests
- Google offers $200 in free monthly credits for most users
Conclusion
This comprehensive Google Maps JavaScript API tutorial has walked you through the complete process of how to extract data from Google Maps using three different methods: geocoding addresses, searching for businesses by category and location, and calculating directions between points.
Whether you choose to use the official Google Maps API or opt for a no-code solution like Scrap.io, you now have the tools and knowledge to start extracting valuable business data from Google Maps for your prospecting and analysis needs.
Key takeaways from this guide:
- Google Maps contains a vast database of business information
- The Google Maps API provides official, legal methods for data extraction
- JavaScript can be used effectively even by non-developers
- Legal compliance and rate limiting are essential considerations
- Alternative tools exist for users who need simpler solutions
Remember to always respect rate limits, follow best practices, and ensure your data extraction methods comply with applicable terms of service and legal requirements.