Python 3.4 googlemaps'模块'对象不可调用

时间:2015-03-22 15:11:36

标签: python google-maps python-3.x

我是编程Python的新手,并且已经做了一些使用下面代码的教程。当我尝试为googlemaps模块定义变量时,我的代码在第二行偶然发现。我从在线教程获得的源代码是从谷歌地图服务器获取路线:https://www.youtube.com/watch?v=PzkHU5GM8_E&list=PLHPcpp4e3JVqYgQFM9NPo_ksEqa_bRH79&t=74

from googlemaps import googlemaps

MS = googlemaps() <-- 'module' object is not callable

我查看了Python 3.4文档区域和模块区域。我曾尝试通过PIP再次下载模块,但无济于事,谷歌也没有提出任何建议。

我确实注意到其他代码有GoogleMaps(有大写字母)我试图调用但是它不存在。 我没有尝试过任何其他版本的Python中的代码,但这似乎是一个非常基本的错误。

任何人都能解释一下吗?

2 个答案:

答案 0 :(得分:0)

该视频适用于以前版本的googlemaps库。在最新版本中,API已更改。如果要安装该(较旧)版本:

pip install googlemaps==1.0.2

然后,您应该将您的导入声明完全写入视频(GoogleMaps)。

答案 1 :(得分:0)

要获取最新版本中的路线,您需要使用Clientdirection

from googlemaps import Client

c = Client(key="api_key")

d = c.directions("texarkana","atlanta")

要获得视频中的步骤:

steps = c.directions("texarkana","atlanta")[0]["legs"][0]["steps"]

for d in steps:
    print(d['html_instructions'])

Head <b>northeast</b> on <b>W 7th St</b> toward <b>Main St</b><div style="font-size:0.9em">Entering Arkansas</div>
Turn <b>right</b> onto <b>US-71 S/Hickory St</b><div style="font-size:0.9em">Continue to follow US-71 S</div>
Turn <b>right</b> onto the ramp to <b>Shreveport</b>
Merge onto <b>I-49 S</b><div style="font-size:0.9em">Entering Louisiana</div>
Continue onto <b>Exit 215</b>
Turn <b>left</b> onto <b>LA-1 S/N Market St</b>
Turn <b>left</b> to merge onto <b>I-220 E</b> toward <b>Monroe</b>
Take exit <b>17B</b> on the <b>left</b> to merge onto <b>I-20 E</b> toward <b>Monroe</b><div style="font-size:0.9em">Entering Mississippi</div>
Keep <b>right</b> at the fork to continue on <b>I-20 E/US 49 S</b>, follow signs for <b>Hattiesburg/Meridian</b><div style="font-size:0.9em">Continue to follow I-20 E</div><div style="font-size:0.9em">Entering Alabama</div>
Take exit <b>106</b> for <b>I-459 N</b> toward <b>Atlanta/Montgomery</b>
Continue onto <b>I-459 N</b>
Take exit <b>29</b> toward <b>Atlanta</b>
Merge onto <b>I-20 E</b><div style="font-size:0.9em">Entering Georgia</div>
Take exit <b>56B</b> for <b>Windsor St</b> toward <b>Spring St/Turner Field</b>
Slight <b>right</b> toward <b>Rawson St SW</b>
Continue onto <b>Rawson St SW</b>
Turn <b>left</b> onto <b>Central Ave SW</b>
Turn <b>right</b> onto <b>Memorial Dr SW</b>
Turn <b>left</b> onto <b>Capitol Ave SW</b>