Skip to content

Instantly share code, notes, and snippets.

@alancwoo
Last active March 9, 2016 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alancwoo/a01d01de25b5954656cd to your computer and use it in GitHub Desktop.
Save alancwoo/a01d01de25b5954656cd to your computer and use it in GitHub Desktop.
Simple python script for retrieving longitude and latitude from Google Location Geocoding API
# geocoder.py
#
# requires googlemaps python package:
#
# 1. pip install googlemaps
# 2. get and set your Api Key: https://developers.google.com/api-client-library/python/guide/aaa_apikeys#introduction
# 3. run with a single search or multiple
#
# geocoder.py 'Toronto, Canada'
# geocoder.py 'Toronto, Canada' 'Paris, France'
#!/usr/local/bin/python
import sys
import googlemaps
gmaps = googlemaps.Client(key='SET_API_KEY_HERE')
locations = sys.argv
locations.pop(0)
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
if len(locations):
def printGeoLocation(location):
geocode_result = gmaps.geocode(location)
if len(geocode_result):
lat = geocode_result[0]['geometry']['location']['lat']
lng = geocode_result[0]['geometry']['location']['lng']
print "%(lat)s, %(lng)s, '%(location)s'" % locals()
else:
print "no_result,no_result,'%(location)s'" % locals()
print "lat,lng,term"
map (printGeoLocation, locations)
else:
print bcolors.HEADER + bcolors.UNDERLINE + "\ngetlnglat.py\n" + bcolors.ENDC + bcolors.WARNING + "\nNo locations given.\n" + bcolors.ENDC + "Please Provide a single search or a list\n\n> geocode.py 'Toronto, Canada'\n> geocode.py 'Toronto, Canada', 'Paris, France'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment