cannot import name GoogleMaps in python
Solution 1
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)
Solution 2
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
Solution 3
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.
Solution 4
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
Solution 5
Andrzej's answer worked for me!
from googlemaps import Client as GoogleMaps
Just to add to what he said, you will need to
- Click on the API Library in your Google Cloud Platform console
- Search for "Geolocation API"
- Click on "Enable"
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.
Related videos on Youtube
Pavan Chakravarthy
Updated on September 16, 2022Comments
-
Pavan Chakravarthy about 1 year
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.
-
Pavan Chakravarthy over 8 yearsdid that & now am getting this error File "C:/Users/Pavan/PycharmProjects/MGCW/latlong6.py", line 2, in <module> gmaps = GoogleMaps(api_key) NameError: name 'api_key' is not defined
-
Scott over 8 years@PavanChakravarthy that's because you need an api. Use geopy instead.
-
Andrzej Pronobis over 8 yearsDid you define the name api_key? It seems that you didn't.
-
Pavan Chakravarthy over 8 yearsthis code is working fine for single location & when am passing multiple locations am getting all latitudes & longitudes same..
-
Andrzej Pronobis over 8 yearsCheck this out for a description of the API key: github.com/googlemaps/google-maps-services-python#api-keys
-
Pavan Chakravarthy over 8 yearsthis seems to work now.. anyhow I need to locate 44,000 locations.. will try to put them in a file & will pass them..
-
Scott over 8 years@PavanChakravarthy I'm not too familiar with the library but they do seem to have nice documentation geopy.readthedocs.org/en/1.10.0
-
Pavan Chakravarthy over 8 yearsas expected the above code worked fine for few locations & if it couldnt find the coordinates it would fail other locations also..
-
Scott over 8 years@PavanChakravarthy Yes geopy seems to not be able to handle some types of addresses. Check out my other answer using photon.komoot.de.
-
Brian about 5 years@Scott slightly off topic, but I am wondering if you know of a way to generate lat longs from zip codes? I am having some trouble locating a good solution for python, thank you!
-
Scott about 5 years@Brian I have had to do that before. You basically need to know the center or some representative lat/lon for the whole zip code. Different sites have compiled extensive lists in csv format from government data. Read it in perhaps as a dictionary with key and zip code and value as object/named tuple that contains lat/lon. Top link right now on google is gist.github.com/erichurst/7882666 containing data from 2013.
-
Brian about 5 years@Scott thanks for the reply. The US ones aren't as big of a pain to handle. I am trying to do this across the world for ~1.2 million zip to zip combinations. I am trying to avoid paying for an API. However it doesn't look like any packages accomplish this at scale.
-
Scott about 5 years@Brian Sorry I don't know of any free service with worldwide data.
-
Mostafa Ghadimi about 4 yearsYour answer is not true. The problem is from the package, not the installation!
-
Admin almost 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.