Skip to content

Instantly share code, notes, and snippets.

@dannvix
Last active December 15, 2015 17:39
Show Gist options
  • Save dannvix/5298170 to your computer and use it in GitHub Desktop.
Save dannvix/5298170 to your computer and use it in GitHub Desktop.
convert latitude & longitude to formatted address with Google Maps API
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys, traceback
import json, urllib2
def latlng_to_addr (lat, lng):
# convert given latitude & longitude to formatted address with Google Maps API
# ref: http://techslides.com/convert-latitude-and-longitude-to-a-street-address/
maps_api_url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false' % (lat, lng)
try:
resp_json = json.loads(urllib2.urlopen(maps_api_url).read())
fmt_addr = resp_json["results"][0]["formatted_address"]
return fmt_addr
except:
traceback.print_exc(file=sys.stderr)
def main (argv):
if len(argv) < 3:
print 'usage: ./%s <lat> <lng>' % argv[0]
exit(1)
lat, lng = argv[1], argv[2]
formatted_address = latlng_to_addr(lat, lng)
print formatted_address
if __name__ == '__main__':
main(sys.argv)
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from geopy import geocoders
import sys, traceback
def latlng_to_addr (lat, lng):
geocoder = geocoders.googlev3.GoogleV3()
place, point = geocoder.geocode('%s,%s' % (lat, lng))
return place
def main (argv):
if len(argv) < 3:
print 'usage: ./%s <lat> <lng>' % argv[0]
exit(1)
lat, lng = argv[1], argv[2]
formatted_address = latlng_to_addr(lat, lng)
print formatted_address
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment