Steven Garboden
Steven Garboden

Reputation: 77

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. 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). I want my definition to return the lat/longs from the address using a for loop. I'm unfamiliar with parsing json:/ Any help appreciated!

Also, would geocode_result need to be json_results from my request results codeblock?

 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

Upvotes: 0

Views: 1598

Answers (1)

Bill Bell
Bill Bell

Reputation: 21663

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

Upvotes: 2

Related Questions