16

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.

6 Answers 6

9

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)
Sign up to request clarification or add additional context in comments.

9 Comments

this code is working fine for single location & when am passing multiple locations am getting all latitudes & longitudes same..
this seems to work now.. anyhow I need to locate 44,000 locations.. will try to put them in a file & will pass them..
@PavanChakravarthy I'm not too familiar with the library but they do seem to have nice documentation geopy.readthedocs.org/en/1.10.0
as expected the above code worked fine for few locations & if it couldnt find the coordinates it would fail other locations also..
@PavanChakravarthy Yes geopy seems to not be able to handle some types of addresses. Check out my other answer using photon.komoot.de.
|
5

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

4 Comments

did 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
@PavanChakravarthy that's because you need an api. Use geopy instead.
Did you define the name api_key? It seems that you didn't.
Check this out for a description of the API key: github.com/googlemaps/google-maps-services-python#api-keys
4

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.

Comments

3

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

Comments

2

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.

Comments

-2
pip install -U googlemaps

use this statement to install and use googlemaps api in Python

1 Comment

Your answer is not true. The problem is from the package, not the installation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.