Skip to content

Instantly share code, notes, and snippets.

@svschannak
Created January 19, 2014 19:42
Show Gist options
  • Save svschannak/8509994 to your computer and use it in GitHub Desktop.
Save svschannak/8509994 to your computer and use it in GitHub Desktop.
Get the name of the city and the street of your location from Google Geocode. Example for Django.
def get_city_json(request, latitude, longitude):
r = requests.get('http://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=true' % (latitude, longitude))
#convert result to json
json_data = r.json()
#get data from json
city = json_data['results'][0]['address_components'][3]['long_name']
response_data = {}
response_data['city'] = city
response_data['street'] = json_data['results'][0]['address_components'][1]['long_name']
#data as json-response
return HttpResponse(json.dumps(response_data), content_type="application/json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment