[Python] How to use the Google Maps Places API (New) in python?

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I want to use the textSearch (https://developers.google.com/maps/...Spicy Vegetarian Food in Sydney, Australia"}}) in Places API (New) to obtain all of the POIs of one specific category in an area, but I'm always getting no return from the code.

    My code:

    import requests

    def get_places_by_category(query, api_key):
    # Base URL for the Text Search API
    base_url = "https://places.googleapis.com/v1/places:searchText"

    # Parameters for the request
    params = {
    "query": query,
    "key": api_key
    }

    # Send the request
    response = requests.get(base_url, params=params)

    # Check if the request was successful
    if response.status_code == 200:
    return response.json()
    else:
    return None

    # Example usage
    api_key = API_KEY # Replace with the actual API key
    query = "restaurant" # Category of POIs you want to search for

    places = get_places_by_category(query, api_key)

    if places:
    for place in places['results']:
    print(f"Name: {place['name']}, Address: {place['formatted_address']}")
    else:
    print("Failed to get a response from Google Places API (New)")


    It keeps returning "Failed to get a response from Google Places API (New)".

    Continue reading...

Compartilhe esta Página