简体   繁体   English

通过Google API从返回的json中提取纬度/经度(地理编码)

[英]Extracting Lat/Long from returned json via google api(Geocoding)

I have a definition which looks like the below block. 我有一个定义,看起来像下面的块。 I think I'm experiencing the same problem. 我想我遇到了同样的问题。 I think the api has updated, and thus the extraction of the lat/long coordinates may be in a slightly different position. 我认为api已更新,因此经/纬度坐标的提取可能会略有不同。 I have made requests successfully by inputing an example address in as a parameter, but I can't get that to work in my def( http://docs.python-requests.org/en/master/user/quickstart/#make-a-request ). 我已经通过输入一个示例地址作为参数成功发出了请求,但是我无法在我的def( http://docs.python-requests.org/en/master/user/quickstart/#make -a请求 )。 I want my definition to return the lat/longs from the address using a for loop. 我希望我的定义使用for循环从地址返回纬度/经度。 I'm unfamiliar with parsing json:/ Any help appreciated! 我不熟悉解析json:/任何帮助表示赞赏!

Also, would geocode_result need to be json_results from my request results codeblock? 另外, 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)

想要从这个位置纬度/经度获取我在定义中传递的地址列表

在此处输入图片说明 在此处输入图片说明

I don't see a difference between what this does and what your code does. 我认为这与您的代码没有区别。 Hope it's of some use to you. 希望对您有所帮助。

>>> 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

EDIT : 编辑

Start with a dataframe of two columns, one with the names of the veterinary hospitals, one with their addresses. 从两列的数据框开始,一列的名称是兽医医院的名称,一列的地址。

>>> 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  

Use .tolist() to obtain the addresses in the form of a list so that they can be passed one at a time to google for their latitudes and longitudes which are stored in the eponymous lists. 使用.tolist()以列表的形式获取地址,以便一次将它们传递给google以获取存储在同义列表中的纬度和经度。 Display the results. 显示结果。

>>> 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]

Now put the results into the dataframe and display the complete result. 现在将结果放入数据框中并显示完整结果。

>>> 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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从python中的BeautifulSoup对象中提取经/纬度 - Extracting lat/long from a BeautifulSoup object in python 从Google Maps API v3趋之若Long - Getting Lat and Long from Google Maps API v3 错误:从geocode_url中提取long时,在Geopy中出现“无法解码JSON对象”的错误 - ERROR: “No JSON object can be decoded” in Geopy when extracting lat, long from geocode_url 如何将Google历史记录JSON中的长整数/经度整数转换为Google Map格式? - How to convert long/lat integers from google history JSON to google map format? 如何将经纬度加长到Google的时区API的响应中? - How do I add the lat and long back into the response from Google's Timezone API? 通过geopy从pandas数据帧中获取所有经度和纬度 - getting all long and lat from pandas dataframe via geopy 如何从 JSON 地图上的 csv 绘制地址(纬度/经度)。? - How to plot addresses (Lat/Long) from a csv on JSON map.? Google Maps Geocoding API没有从完整的地址列表中返回坐标 - Google Maps Geocoding API is not returning coordinates from the complete list of addresses Google place API(地理编码)的搜索结果不正确 - Inaccurate search result from Google place API (Geocoding) 从 Python 中的 Google Geocoding API 获取特定字段 - Get Specific fields from Google Geocoding API in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM