通过google api(Geocoding)从返回的json中提取Lat / Long

时间:2017-08-15 18:54:35

标签: python json google-maps-api-3

我有一个看起来像下面的块的定义。我想我遇到了同样的问题。我认为api已经更新,因此纬度/经度坐标的提取可能处于稍微不同的位置。我通过输入示例地址作为参数成功地提出了请求,但是我无法在我的def(http://docs.python-requests.org/en/master/user/quickstart/#make-a-request)中使用它。我希望我的定义使用for循环从地址返回lat / longs。我对解析json不熟悉:/任何帮助表示赞赏!

此外,我的请求结果代码块中geocode_result是否需要json_results

 def geocode_address(loc):
        gmaps = googlemaps.Client(key= creds.GOOGLE_MAPS['api_key'])
        geocode_result = gmaps.geocode(loc)
        lat = json_results[0]["geometry"]["location"]["lat"]
        lon = json_results[0]["geometry"]["location"]["lng"]
        print (lat,lon)

wanted lat/long from this position for a list of addresses I pass in in my definition

enter image description here enter image description here

1 个答案:

答案 0 :(得分:2)

我没有看到它与您的代码之间的区别。希望它对你有用。

>>> import requests
>>> payload = {'key':}
>>> base_url = 'https://maps.googleapis.com/maps/api/geocode/json'
>>> payload = {'address': '1845 E. Broadway Road Ste. 102, Tempe, AE, 85282'}
>>> r = requests.get(base_url, params=payload)
>>> r
<Response [200]>
>>> coords = r.json()['results'][0]['geometry']['location']
>>> coords['lat']
33.406601
>>> coords['lng']
-111.9075196

修改

从两列数据框开始,一列是兽医医院的名称,另一列是地址。

>>> import pandas as pd
>>> df
                            0  \
0           The Animal Clinic   
1  Sherbourne Animal Hospital   
2     Spadina Animal Hospital   
3   Wellesley Animal Hospital   
4      Cabbagetown Pet Clinic   

                                                   1  
0            106 Mutual St  Toronto  Ontario M5B 2R7  
1  320 Richmond Street East Unit 8  Toronto  Onta...  
2       125 Spadina Avenue  Toronto  Ontario M5V 2K8  
3         8 Wellesley St W  Toronto  Ontario M4Y 1E7  
4         239 Gerrard St E  Toronto  Ontario M5A 2G1  

使用.tolist()以列表形式获取地址,以便一次一个地传递给谷歌,以获取存储在同名列表中的纬度和经度。显示结果。

>>> import requests
>>> base_url = 'https://maps.googleapis.com/maps/api/geocode/json'
>>> latitudes = []
>>> longitudes = []
>>> for address in df[1].tolist():
...     payload = {'address': address}
...     r = requests.get(base_url, params=payload)
...     coords = r.json()['results'][0]['geometry']['location']
...     latitudes.append(coords['lat'])
...     longitudes.append(coords['lng'])
...     
>>> latitudes
[43.6572571, 43.6535161, 43.6472168, 43.6650199, 43.6617416]
>>> longitudes
[-79.37609119999999, -79.3688681, -79.39527749999999, -79.3851912, -79.369494]

现在将结果放入数据框并显示完整的结果。

>>> df['latitudes'] = latitudes
>>> df['longitudes'] = longitudes
>>> df
                            0  \
0           The Animal Clinic   
1  Sherbourne Animal Hospital   
2     Spadina Animal Hospital   
3   Wellesley Animal Hospital   
4      Cabbagetown Pet Clinic   

                                                   1  lat  latitudes  \
0            106 Mutual St  Toronto  Ontario M5B 2R7  -31  43.657257   
1  320 Richmond Street East Unit 8  Toronto  Onta...  -42  43.653516   
2       125 Spadina Avenue  Toronto  Ontario M5V 2K8  -20  43.647217   
3         8 Wellesley St W  Toronto  Ontario M4Y 1E7   19  43.665020   
4         239 Gerrard St E  Toronto  Ontario M5A 2G1   50  43.661742   

   longitudes  
0  -79.376091  
1  -79.368868  
2  -79.395277  
3  -79.385191  
4  -79.369494