[英]How do I convert two floats of latitude and longitude into Google Maps latlng?
我有数据框中地点的纬度和经度。 我希望使用 Google Maps API 获取邮政编码。 现在我正在尝试编写一个循环来执行此操作,但它不起作用。 问题是 gmaps 变量latlng
如何将我的变量的两个浮点数转换为一个变量latlng
?
我已经成功地做到了这一点,我在坐标中进行了“硬编码”。
for index, row in bike_df.iterrows():
lat = row["Starting Station Latitude"]
long = row["Starting Station Longitude"]
target_url = ('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng=34.052898,-118.241562&key={0}').format(gkey)
response = requests.get(target_url).json()
try:
print(response['results'][0]['address_components'][7]['long_name'])
except:
print(f"Could not {index} find zip")
for index, row in bike_df.iterrows():
lat = row["Starting Station Latitude"]
long = row["Starting Station Longitude"]
target_url = ('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng=lat,long&key={0}').format(gkey)
response = requests.get(target_url).json()
try:
print(response['results'][0]['address_components'][7] .
['long_name'])
except:
print(f"Could not {index} find zip")
它只是运行和运行,没有任何输出。
您的lat
和long
变量未插入target_url
字符串中。 您需要执行以下操作:
target_url = ('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng={lat},{long}&key={gkey}').format(lat=lat,long=long,gkey=gkey)
您应该像使用 gkey 一样使用连接变量。 您可以通过多种方式做到这一点。
例如:
target_url = ('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng='+str(lat)+','+str(long)+'&key={0}').format(gkey)
或类似
target_url = ('https://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}&key={2}').format(lat, long, gkey)
也许试试这个?
import pandas as pd
df = pd.DataFrame({'lat':[34.052898,34.052898,34.052898],
'long':[-118.241562,-118.241562,-118.241562]})
df
for index, row in df.iterrows():
latlng = lat + long
print(latlng)
[34.052898, -118.241562] [34.052898, -118.241562] [34.052898, -118.241562]
lat = row["Starting Station Latitude"]
long = row["Starting Station Longitude"]
target_url = ('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng=lat,long&key={0}').format(gkey)
response = requests.get(target_url).json()
try:
print(response['results'][0]['address_components'][7] .
['long_name'])
except:
print(f"Could not {index} find zip")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.