Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Latitude, Longitude to Geo Location
# depends on Google's API.
# So, YMMV
import sys
import pycurl
import simplejson as json
class RevCode:
def __init__(self):
self.contents = ''
def body_callback(self, buf):
self.contents = self.contents + buf
def get_address(self, latlong):
c = pycurl.Curl()
c.setopt(c.URL, 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latlong+'&sensor=false')
c.setopt(c.WRITEFUNCTION, self.body_callback)
c.perform()
c.close()
addresses = json.loads(self.contents)
types_map = {
'street_address': 'location',
'route': 'location',
'intersection': 'location',
'locality': 'location',
'sublocality': 'location',
'neighborhood': 'location',
'premise': 'location',
'subpremise': 'location',
'street_number': 'location',
'administrative_area_level_3': 'city',
'administrative_area_level_2': 'city',
'administrative_area_level_1': 'state',
'country': 'country'
}
locations = {}
for address in addresses['results']:
for ty in types_map.keys():
try:
if (address['types'].index(ty) >= 0):
address_component = repr(address['address_components'][0]['long_name']).replace("'", '')
# if (locations[types_map[ty]]):
# locations[types_map[ty]] += address_component
# else:
locations[types_map[ty]] = address_component
except Exception, e:
pass
return locations
if __name__ == "__main__":
latlong = '12.3456,77.60704'
r = RevCode()
print r.get_address(latlong)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment