1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #
# models.py
#
import urllib
import settings
def get_lat_long(location):
key = settings.GOOGLE_API_KEY
output = "csv"
location = urllib.quote_plus(location)
request = "http://maps.google.com/maps/geo?q=%s&output=%s&key=%s" % (location, output, key)
data = urllib.urlopen(request).read()
dlist = data.split(',')
if dlist[0] == '200':
return "%s, %s" % (dlist[2], dlist[3])
else:
return ''
|
More like this
- Google v3 geocoding for Geodjango admin site by samhag 6 months, 2 weeks ago
- Retrieve Latitude & Longitude for an Address from Google Geocoder V3 by whardeman 2 years, 6 months ago
- Get Latitude and Longitude from google maps by b23 5 years, 5 months ago
- widget to capture a geographic Point by jerojasro 5 years, 2 months ago
- Google Maps Templatetag by javinievas 5 years, 10 months ago
Comments
get problems with : location = urllib.quote_plus(location)
when location has some accents
#
solution : location = smart_str(urllib.quote_plus(location))
#
does this return "lat, lon" or "lon, lat" ?
#
P.S. great snippet, thanks
#
@falsh ...you mean:
location = urllib.quote_plus(smart_str(location))
(the other way still throws errors if you have foreign chars)
#
Good stuff...now that Google have released the v3 api, this could be simplified a bit since key is no longer required. Parsing the JSON output should be easier as well...
http://code.google.com/apis/maps/documentation/geocoding/
#
FYI: This snippet uses v3 gmaps API. I was in the process or re-writing this code to work with v3 and JSON, but it looks like someone beat me to it.
#