How to get address coordinates from Python

I need to geocode the address in a couple of latitudes and longitudes to display on Google Maps, but I need to make this server part in Django. I can find a link to the Javascript V3 API. How to do it from Python?

+12
source share
5 answers

I would suggest using Py-Googlemaps . It is easy to use:

from googlemaps import GoogleMaps gmaps = GoogleMaps(API_KEY) lat, lng = gmaps.address_to_latlng(address) 

UPDATE: If necessary, install Py-Googlemaps via: sudo easy_install googlemaps .

+14
source

I highly recommend using geopy . It will return latitude and longitude, after which you can use it in the Google JS client.

 >>> 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) 

In addition, you can specifically determine that you want to use Google services using the GoogleV3 class as a geolocation

 >>> from geopy.geocoders import GoogleV3 >>> geolocator = GoogleV3() 
+13
source

Google Data has an API for Maps has a REST-ful API - and they also have

+5
source

Here is the code that works for the 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 the official documentation for a complete list of options and additional information.

0
source

The Python googlemaps seems very capable of doing geocoding and reverse geocoding.

The latest version of the googlemaps package is available at: https://pypi.python.org/pypi/googlemaps

0
source

All Articles