Skip to content

Instantly share code, notes, and snippets.

@mattdennewitz
Created January 6, 2010 22:06
Show Gist options
  • Save mattdennewitz/270720 to your computer and use it in GitHub Desktop.
Save mattdennewitz/270720 to your computer and use it in GitHub Desktop.
# quick & dirty example
def get_placemark_point(raw_location):
"""Attempt to resolve ``raw_location`` using Google's geocoder.
Returns a tuple with the canonical address and GEOS point
"""
import cjson
from urllib import urlencode
from urllib2 import urlopen
from django.contrib.gis.geos import fromstr
# encode the request
data = urlencode({
'q': raw_location,
'output': "json",
'oe': "utf8",
'sensor': "false",
'key': "my_app_key"
})
url = "http://maps.google.com/maps/geo?%s" % data
response = urlopen(url)
geo_content = cjson.decode(response.read())
placemark = geo_content['Placemark'][0]
# convert to a geodjango point
lng, lat = placemark['Point']['coordinates'][:2]
point = fromstr("POINT(%s %s)" % (lng, lat))
return (placemark['address'], point)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment