[英]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.