[英]How do I convert two floats of latitude and longitude into Google Maps latlng?
I have the latitude and longitude of places in a dataframe.我有数据框中地点的纬度和经度。 I am looking to get the zip code using Google Maps API.
我希望使用 Google Maps API 获取邮政编码。 Now I am trying to write a loop to do this and it won't work.
现在我正在尝试编写一个循环来执行此操作,但它不起作用。 The problem is the gmaps variable
latlng
How do I get two floats that are my variables into one variable that is latlng
?问题是 gmaps 变量
latlng
如何将我的变量的两个浮点数转换为一个变量latlng
?
I have successfully done this went I "hardcode" in the coordinates.我已经成功地做到了这一点,我在坐标中进行了“硬编码”。
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")
It just runs and runs without any output.它只是运行和运行,没有任何输出。
Your lat
and long
variables aren't being inserted in the target_url
string.您的
lat
和long
变量未插入target_url
字符串中。 You'll need to do something like:您需要执行以下操作:
target_url = ('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng={lat},{long}&key={gkey}').format(lat=lat,long=long,gkey=gkey)
You should use, like you did with gkey, concatate your variable.您应该像使用 gkey 一样使用连接变量。 You can do it in several way.
您可以通过多种方式做到这一点。
For example:例如:
target_url = ('https://maps.googleapis.com/maps/api/geocode/json?'
'latlng='+str(lat)+','+str(long)+'&key={0}').format(gkey)
or somehting like或类似
target_url = ('https://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}&key={2}').format(lat, long, gkey)
Maybe try this?也许试试这个?
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]
[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.