Skip to content

Instantly share code, notes, and snippets.

@damiann
Last active January 3, 2016 07:09
Show Gist options
  • Save damiann/8427619 to your computer and use it in GitHub Desktop.
Save damiann/8427619 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python2.7
import urllib2, json
def get_coorinates(request):
eat_all = Eat.objects.all().values()
for eat in eat_all:
obj = Eat.objects.filter(id=eat['id'])[0]
address = obj.address.replace(" ", "+")
url = "".join(["http://maps.googleapis.com/maps/api/geocode/json?address=", address, "&sensor=false"]);
response = urllib2.urlopen(url);
data = json.loads(response.read())
result = data.get("results",{})
geometry = [ geometry['geometry'] for geometry in result ]
location = [ location['location'] for location in geometry ]
for x in location:
obj.lat_coordinates = str(x['lat'])
obj.lng_coordinates = str(x['lng'])
print obj
obj.save()
return HttpResponse({'success': True},content_type='application/json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment