使用 Google Places API 在英国获取所有机制的最佳方式是什么?

问题描述 投票:0回答:0

我试图通过指定一个覆盖英国大部分地区的大半径位置并请求该范围内的所有机制来做到这一点,但我只得到 60 个结果,这当然比实际情况少得多。所以我认为它的行为可能与您使用 Google 地图时的行为类似,其中当缩放太远时它不会显示所有结果,因为它太多了。当时我想做一个循环,以小半径遍历英国的每个区域并收集这样的数据,但这似乎是一种缓慢且可能不准确的方法,有什么建议吗?

这是我试过的代码:

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 = "AIzaSyB6xP_vv47QGyjsaLD_hk0pcKHvv7593G4" # 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)

代码所做的是指定一个覆盖英国大部分地区的大半径圆(我的想法是对多个圆重复这个直到我覆盖整个英国)并请求该圆的力学数据但我发现似乎最多只能达到 60.

api google-places-api
© www.soinside.com 2019 - 2024. All rights reserved.