Query google geocoding API with python

  1. Create your application’s API key:
  • Go to the Google API Console https://console.cloud.google.com/apis/dashboard
  • From the projects list, select a project or create a new one.
  • If the APIs & services page isn’t already open, open the left side menu and select APIs & services.
  • On the left, choose Credentials.
  • Click Create credentials and then select API key.
  1. Enable Geocoding API:
  • Log in to the Google API Manager Console here: https://console.developers.google.com/apis/library
  • Click the Library link in the left sidebar
  • Select the project you created when you created your API Key for WP Google Maps (See the top arrow in the screenshot below)
  • Click the link to the Google Maps Geocoding API
  • Click Enable on the Google Maps Geocoding API window.
  1. Verify your KEY in browser https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=your-key
  1. Create geocode.py
import json
import time
import urllib.error
import urllib.request
import urllib.parse

api_file = 'your-key'
base_url = "https://maps.googleapis.com/maps/api/geocode/json"

def geoapi(address):
    params = urllib.parse.urlencode({"address": address, "key": api_file})
    url = f"{base_url}?{params}"
    response = urllib.request.urlopen(url)
    result = json.load(response)
    return result

if __name__ == "__main__":
    goecode = geoapi('3600 Steeles Ave E, Markham, ON')
    print(goecode)
  1. run it with python geocode.py

6 Replies to “Query google geocoding API with python”

  1. Excellent post. I used to be checking continuously this weblog and I
    am inspired! Extremely helpful information specifically the ultimate part
    🙂 I maintain such info much. I used to be seeking this
    particular information for a long time. Thanks and best of luck.

    Here is my website :: Follipur Reviews

Leave a Reply

Your email address will not be published. Required fields are marked *