geoJson API – finding place ID

import urllib.parse
import urllib.request
import json

api_url="https://maps.googleapis.com/maps/api/geocode/json?"

# Function to retrieve place ID for a given Location
def get_place_id(location):
    params = {
        'address': location,
        'key': '.........'  # Replace 'YOUR_API_KEY' with your actual API key
    }

    url = api_url + urllib.parse.urlencode(params)

    try:
        response = urllib.request.urlopen(url)
        data = response.read().decode()
        json_data = json.loads(data)

        # Debugging: Print the JSON data from the response
        print(json_data)

        if 'results' in json_data and len(json_data['results']) > 0:
            place_id = json_data['results'][0]['place_id']
            return place_id
        else:
            return 'Place ID not found'

    except urllib.error.URLError as e:
        return f'An error occurred: {e}'

# Prompt for a Location
location = input('Enter a location: ')

# Call the function to get the place ID
place_id = get_place_id(location)

# Print the place ID
print('Place ID:', place_id)

I am trying to find the place ID however I get an error message. I tried generating a new API key and enabling Java API map and place map from Google console but I still get an error.

  • 2

    Maybe the error message is telling you what the problem is?

    – 

Leave a Comment