簡體   English   中英

如何將緯度和經度的兩個浮點數轉換為 Google Maps latlng?

[英]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")

它只是運行和運行,沒有任何輸出。

您的latlong變量未插入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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM