2015-05-29 25 views
14

导入名称的GoogleMaps我使用下面的代码来获得一个地址的纬度&经度:不能在python

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 

但我得到以下

File "C:/Users/Pavan/PycharmProjects/MGCW/latlong6.py", line 1, in <module> 
    from googlemaps import GoogleMaps 
ImportError: cannot import name GoogleMaps 

错误我已经看到了另一个问题与此类似,但该解决方案对我无效。

回答

6

使用geopy代替,不需要API密钥。

从他们的例子:

from geopy.geocoders import Nominatim 
geolocator = Nominatim() 
location = geolocator.geocode("175 5th Avenue NYC") 
print(location.address) 
print((location.latitude, location.longitude)) 

打印:

Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, 10010, United States of America 
(40.7410861, -73.9896297241625) 
+1

此代码工作正常单个位置&当我通过多个位置获取所有纬度和经度相同.. –

+1

这似乎现在工作..无论如何,我需要找到44,000个位置..将尝试把它们放在一个文件中,并将通过它们.. –

+0

@PavanChakravarthy我不太熟悉库,但他们似乎有很好的文档https://geopy.readthedocs .org/en/1.10.0/ – Scott

3

我认为你在找什么是Client类不是GoogleMaps

如果你想叫它GoogleMaps如下导入:

from googlemaps import Client as GoogleMaps

+1

这样做,现在和我得到这个错误 文件 “C:/Users/Pavan/PycharmProjects/MGCW/latlong6.py”,2号线,在 gmaps =谷歌地图( api_key) NameError:name'api_key'is没有定义 –

+0

@PavanChakravarthy这是因为你需要一个API。改用geopy。 – Scott

+0

您是否定义了名称api_key?看来你没有。 –

4

另一种选择是从photon.komoot.de解析JSON。例如:

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'] 

打印:

[-76.1438449, 40.229888] 
[-77.046567, 38.8924587] 

这些在经度,纬度给出。第二个是约1英里的一点点。似乎街道交叉口很困难。