Reputation: 593
I am using the code below to get the latitude & longitude of an address:
from googlemaps import GoogleMaps
gmaps = GoogleMaps(api_key)
address = 'Constitution Ave NW & 10th St NW, Washington, DC'
lat, lng = gmaps.address_to_latlng(address)
print lat, lng
but am getting the error below
File "C:/Users/Pavan/PycharmProjects/MGCW/latlong6.py", line 1, in <module>
from googlemaps import GoogleMaps
ImportError: cannot import name GoogleMaps
I have seen another question similar to this, but the solution didn't work for me.
Upvotes: 16
Views: 15822
Reputation: 222
Andrzej's answer worked for me!
from googlemaps import Client as GoogleMaps
Just to add to what he said, you will need to
If you create an API key before enabling this, you will also get that error. I had the same issue.
Under the "Credentials" tab, you will then see the option to create or copy your API key.
Upvotes: 2
Reputation: 7
pip install -U googlemaps
use this statement to install and use googlemaps api in Python
Upvotes: -2
Reputation: 593
I could write a code for multiple address but it never worked.. Finally found this website which could generate geocodes in bulk.. I think it may be useful to someone looking for bulk geocodes.. It also has reverse geocoding..
http://www.findlatitudeandlongitude.com/batch-geocode/#.VW2KRs-qqkq
Upvotes: 3
Reputation: 6389
Another option is parsing the json from photon.komoot.de. Example:
import requests, json
url = 'http://photon.komoot.de/api/?q='
addresses = ['175 5th Avenue NYC', 'Constitution Ave NW & 10th St NW, Washington, DC']
for address in addresses:
resp = requests.get(url=url+address)
data = json.loads(resp.text)
print data['features'][0]['geometry']['coordinates']
prints:
[-76.1438449, 40.229888]
[-77.046567, 38.8924587]
These are given in lon, lat. The second one is a bit off by about 1 mile. Seems street intersections are difficult.
Upvotes: 4
Reputation: 6389
Use geopy instead, no need for api-key.
From their example:
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("175 5th Avenue NYC")
print(location.address)
print((location.latitude, location.longitude))
prints:
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, 10010, United States of America
(40.7410861, -73.9896297241625)
Upvotes: 9
Reputation: 36106
I think what you are looking for is the Client
class not GoogleMaps
.
If you want to call it GoogleMaps
import it as follows:
from googlemaps import Client as GoogleMaps
Upvotes: 5