gpt4 book ai didi

python googlemaps不同位置之间所有可能的距离

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:26 25 4
gpt4 key购买 nike

schools=['GSGS','GSGL','JKG','JMG','MCGD','MANGD','SLSA','WHGR','WOG','GCG','LP', 
'PGG', 'WVSG', 'ASGE','CZG', 'EAG','GI']

for i in range (1,17):
gmaps = googlemaps.Client(key='')
distances = gmaps.distance_matrix((GSGS), (schools), mode="driving"['rows'][0]['elements'][0]['distance']['text']
print(distances)

列表的元素是学校。我不想让列表太长,所以我使用了这些缩写。我想获取“GSGS”与列表中学校之间的所有距离。我不知道第二个括号内该写什么。

distances = gmaps.distance_matrix((GSGS), (schools)

如果我这样运行它,它会输出以下错误:

Traceback (most recent call last):
File "C:/Users/helpmecoding/PycharmProjects/untitled/distance.py", line 31, in
<module>
distances = gmaps.distance_matrix((GSGS), (schools), mode="driving")['rows'][0]['elements'][0]['distance']['text']
KeyError: 'distance'

我可以一对一地做,但这不是我想要的。如果我从学校列表中写入另一所学校并删除 for 循环,它就可以正常工作。

我知道我必须执行一个循环,以便它在列表中循环,但我不知道该怎么做。每个变量(例如“GSGS”)后面都是学校的地址/位置。为了安全起见,我删除了 key 。

最佳答案

我爸爸帮助了我,我们解决了问题。现在我有了我想要的:)现在我必须列出学校之间的所有距离。如果我明白了,我必须使用 Dijkstra 算法来找到它们之间的最短路线。感谢您的帮助!

import googlemaps

GSGS = (address)
GSGL = (address)
. . .
. . .
. . .

schools =
(GSGS,GSGL,JKG,JMG,MCGD,MANGD,SLSA,WHGR,WOG,GCG,LP,PGG,WVSG,ASGE,CZG,EAG,GI)

school_names = ("GSGS","GSGL","JKG","JMG","MCGD","MANGD","SLSA","WHGR","WOG","GCG","LP","PGG","WVSG","ASGE","CZG","EAG","GI")

school_distances = ()
for g in range(0,len(schools)):
n = 0
for i in schools:
gmaps = googlemaps.Client(key='TOPSECRET')
distances = gmaps.distance_matrix(schools[g], i)['rows'][0]['elements'][0]['distance']['text']
if school_names[g] != school_names[n]:
print(school_names[g] + " - " + school_names[n] + " " + distances)
else:
print(school_names[g] + " - " + school_names[n] + " " + "0 km")
n = n + 1

关于python googlemaps不同位置之间所有可能的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47188367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com