19π
I would suggest using Py-Googlemaps. To use it is easy:
from googlemaps import GoogleMaps
gmaps = GoogleMaps(API_KEY)
lat, lng = gmaps.address_to_latlng(address)
EDIT: If necessary, install Py-Googlemaps
via: sudo easy_install googlemaps
.
27π
I would strongly recommend to use geopy. It will return the latitude and longitude, you can use it in the Google JS client afterwards.
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("175 5th Avenue NYC")
>>> print(location.address)
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, ...
>>> print((location.latitude, location.longitude))
(40.7410861, -73.9896297241625)
Additionally you can specifically define you want to use Google services by using GoogleV3
class as a geolocator
>>> from geopy.geocoders import GoogleV3
>>> geolocator = GoogleV3()
6π
Google Data has an API for Maps has a REST-ful API β and they also have a Python library built around it already.
- Django m2m form save " through " table
- Maintain SQL operator precedence when constructing Q objects in Django
- Is there a way to render a html page without view model?
- Converting a django ValuesQuerySet to a json object
0π
Here is the code working for Google Maps API v3 (based on this answer):
import urllib
import simplejson
googleGeocodeUrl = 'http://maps.googleapis.com/maps/api/geocode/json?'
def get_coordinates(query, from_sensor=False):
query = query.encode('utf-8')
params = {
'address': query,
'sensor': "true" if from_sensor else "false"
}
url = googleGeocodeUrl + urllib.urlencode(params)
json_response = urllib.urlopen(url)
response = simplejson.loads(json_response.read())
if response['results']:
location = response['results'][0]['geometry']['location']
latitude, longitude = location['lat'], location['lng']
print query, latitude, longitude
else:
latitude, longitude = None, None
print query, "<no results>"
return latitude, longitude
See official documentation for the complete list of parameters and additional information.
- Is there any way to use GUIDs in django?
- FileField Size and Name in Template
- How do I get the django HttpRequest from a django rest framework Request?
- External django redirect with POST parameters
- "Returning to that page might cause any action you took to be repeated" β Django
0π
Python package googlemaps
seems to be very able to do geocoding and reverse geocoding.
The latest version of googlemaps package is available at: https://pypi.python.org/pypi/googlemaps
- Django with system timezone setting vs user's individual timezones
- How to have a "random" order on a set of objects with paging in Django?
- Select Children of an Object With ForeignKey in Django?
- Display query string values in django templates