简体   繁体   English

如何将纬度和经度的两个浮点数转换为 Google Maps latlng?

[英]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.您的latlong变量未插入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.

相关问题 如何将地理列转换为纬度和经度? - How do I convert a geography column to latitude and longitude? 如何将 WKT 格式的坐标转换为纬度和经度列 - How do I convert coordinates in WKT format to columns of Latitude and Longitude 无法将“熊猫”列中的IP地址转换为latlng(经度) - Unable to convert IP address in Pandas column to latlng (Latitude Longitude) 如何在Python中为Bokeh Google Maps图设置经度和纬度范围? - How to set longitude and latitude ranges for Bokeh Google Maps plot in Python? 从谷歌地图中抓取城市的经度和纬度 - Scraping longitude and latitude of a city from Google Maps 我想从 dxf 文件和经度、纬度转换一个 geojson。 我们该怎么做? - I want to convert a geojson from dxf file and longitude, latitude. How can we do that? 如何从 python 的经度和纬度获取邮政编码? - How do I get zipcodes from longitude and latitude on python? Django:如何从Google Maps用户选择的地址获取经度和纬度并保存到数据库 - Django : How to get longitude and latitude from google maps user selected adress and save to database 如何将经纬度转换为国家或城市? - how to convert from longitude and latitude to country or city? 如何将经度和纬度转换为街道地址 - How to convert longitude and latitude to street address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM