Skip to content

Instantly share code, notes, and snippets.

@thequbit
Created December 6, 2013 19:51
Show Gist options
  • Save thequbit/7831084 to your computer and use it in GitHub Desktop.
Save thequbit/7831084 to your computer and use it in GitHub Desktop.
Use Map Quest to geocode an address. You will need to sign up for a key here: http://developer.mapquest.com/
def geocodemq(address):
key = ""
vals = {'location': address}
qstr = urllib.urlencode(vals)
#print "QSTR = '{0}'".format(qstr)
reqstr = "http://www.mapquestapi.com/geocoding/v1/address?key={0}&outFormat=json&maxResults=1&{1}".format(key,qstr)
#print "Sending: {0}".format(reqstr)
_json = simplejson.loads(urllib.urlopen(reqstr).read())
return _json
def pulldatamq(_json):
print _json
fulladdress = ""
lat = ""
lng = ""
zipcode = ""
streetnumber = ""
route = ""
locality = ""
state = ""
country = ""
try:
#print _json['results'][0]
#treet = _json['results'][0]['locations'][0]['street']
town = _json['results'][0]['locations'][0]['adminArea5']
state = _json['results'][0]['locations'][0]['adminArea3']
zipcode = _json['results'][0]['locations'][0]['postalCode'].split('-')[0]
fulladdress = ", {0}, {1}, {2}".format(town,state,zipcode)
lat = _json['results'][0]['locations'][0]['latLng']['lat']
lng = _json['results'][0]['locations'][0]['latLng']['lng']
route = _json['results'][0]['locations'][0]['street'].split(" ")[0]
route = "".join(_json['results'][0]['locations'][0]['street'].split(" ")[1:])
locality = _json['results'][0]['locations'][0]['adminArea5']
country = "US"
valid = True
except:
valid = False
retval = valid,fulladdress,lat,lng,zipcode,streetnumber,route,locality,state,country
print retval
return retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment