使用python提取纬度和经度

问题描述 投票:0回答:1

我是python的初学者,正尝试从实际地址中提取纬度和经度。我看过一些视频,可以帮助我编写以下脚本,但是显示

时出现错误

Lat = Geocode_Result [0] [“ geometry”] [“ Location”] [“ Lat”]

KeyError:'Location'

我有两个请求并正在寻求帮助:

1-修复错误

2-无需使用Google API服务(换句话说,使用免费的免费服务即可获得纬度和经度)来提取纬度和经度]

      import pandas as pd
      import googlemaps code
      df=pd.DataFrame({'Address':['9 RUE DU FOSSE, L-7772, luxembourg', '37 RUE DE LA GARE,
      L-7535, luxembourg']})      # Creating a dataframe with 2 addresses (its just an example)              
      Gmaps_key=googlemaps.Client("xxxxxxxxx")

      df['Latitude'] = None
      df['Longitude'] = None

      for i in range(len(df)):
       Geocode_Result=Gmaps_key.geocode(df.loc[i,'Address'])  # sending a request for each of the addresses

      Lat=Geocode_Result[0]["geometry"]["Location"]["Lat"]  # Extracting the Latitude information
      Long=Geocode_Result[0]["geometry"]["Location"]["Long"] # Extracting the Longitude information
      df.loc[i,'Latitude'] = Lat   # Pass the information into the DataFrame
      df.loc[i,'Longitude'] = Long

     print(df)   
python geocoding latitude-longitude
1个回答
0
投票

错误是说Location中没有键Geocode_Result[0]["geometry"]。尝试打印对象?

print(Geocode_Result[0]["geometry"])

文档对您得到的回应怎么说?

还尝试使用location代替Location吗?

© www.soinside.com 2019 - 2024. All rights reserved.