Search code examples
pythongoogle-mapsgoogle-places-api

What is the best way to get all of the mechanics in the UK using Google Places API?


I have tried to do this by specifying a location with a large radius that covers a lot of the UK and requesting all the mechanics in that range, but I only got 60 results which is of course much less than the reality. So I thought it's probably behaving similarly to when you use Google Maps in which it doesn't display all results when the zoom is too far out because it would be too many. I was thinking then to make a loop that goes through every area in the UK with a small radius and collect the data like that but it seems like a bad slow and maybe inaccurate method, any suggestions?

Here is the code I have tried:

import requests
import time
import json

# Set parameters for Places API request
location = "52.5,-1.7" # Replace with desired coordinates
radius = 268000 # Replace with desired radius in meters
type = "car_repair" # Replace with desired business type
key = "API_KEY" # Replace with your API key
pagetoken = ""

# Send request to Places API to retrieve list of place IDs
place_ids = []

while True:
    # Add delay to avoid hitting query rate limit
    time.sleep(2)
    
    # Send request to Places API and retrieve results
    url = f"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={location}&radius={radius}&type={type}&key={key}&pagetoken={pagetoken}"
    response = requests.get(url)
    results = response.json()["results"]
    place_ids += [result["place_id"] for result in results]

    # Check if there are more results to retrieve
    print("yep")
    if "next_page_token" in response.json():
        pagetoken = response.json()["next_page_token"]
    else:
        break

# Retrieve detailed information for each place using Place Details API
data = []
places_data = []
for place_id in place_ids:
    # Add delay to avoid hitting query rate limit
    time.sleep(2)
    # Send request to Place Details API and retrieve result
    url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={place_id}&fields=name,formatted_address,formatted_phone_number,rating,review,opening_hours&key={key}"
    response = requests.get(url)
    result = response.json()["result"]
    data.append(result)

with open("places.json", "w") as outfile:
    json.dump(data, outfile)

# Print results
print(data)

What the code does is specify a circle of a large radius covering a large part of the UK (my idea was to just repeat this for multiple circles till I have covered the entire UK) and requests the mechanics data with that circle but I found it seems it only gets a max of 60.


Solution

  • Places API Limitation

    There's a long standing and popular Feature Request in the Google Public Issue Tracker that specifies returning more than 60 results from the Places API.

    You can check the Feature Request here: https://issuetracker.google.com/issues/35826799

    Please do note that the Issue Tracker is the authoritative source for public information regarding the feature requests, and all publicly-relevant updates will be posted there. So you can post your use case there and star it to get updates.

    The Feature Request was previously closed with the following from comment#14:

    "Unfortunately Places API is not in a position to return more than 60 results.

    Besides technical reasons (latency, among others) returning more than 60 results would make the API be more like a database or general-purpose search engine. We'd rather improve search quality so that users don't need to go so far down a long list of results."

    But it was reopened for some reason. There was also a similar issue filed on one of the Google Maps Github repo here: https://github.com/googlemaps/google-maps-services-java/issues/624

    And the response was this:

    "This is a limitation of the API to avoid scraping. It never returns more than 60 results for a given query."

    There was also something called Radar Search but from what I know, it was already deprecated.

    Hope these information helped!