Search code examples
pythongoogle-mapsgeocodinggoogle-geocoding-apigeopy

Python Google Maps Geocoding API


I am using Google's Geocoding API using Python Libraries (tried GeoPy 1.11.0). For a query such as "Utica" I'm getting a single response:

{u'geometry': {u'location_type': u'APPROXIMATE', u'bounds': {u'northeast': 
{u'lat': 43.132269, u'lng': -75.1609059}, u'southwest': {u'lat': 
43.0644689, u'lng': -75.295296}}, u'viewport': {u'northeast': {u'lat': 
43.132269, u'lng': -75.1609059}, u'southwest': {u'lat': 43.0644689, u'lng': 
-75.295296}}, u'location': {u'lat': 43.100903, u'lng': -75.232664}}, 
u'address_components': [{u'long_name': u'Utica', u'types': [u'locality', 
u'political'], u'short_name': u'Utica'}, {u'long_name': u'Oneida County', 
u'types': [u'administrative_area_level_2', u'political'], u'short_name': 
u'Oneida County'}, {u'long_name': u'New York', u'types': 
[u'administrative_area_level_1', u'political'], u'short_name': u'NY'}, 
{u'long_name': u'United States', u'types': [u'country', u'political'], 
u'short_name': u'US'}], u'place_id': u'ChIJKXZqNVE32YkRhvztGCY2GhE', 
u'formatted_address': u'Utica, NY, USA', u'types': [u'locality', 
u'political']}

The problem is that there are multiple Utica's in the US, for example Utica, NY and Utica, WI. I was expecting Google to return all possible places that have Utica in their name, is there a way to do this?

Here is the code utilizing GeoPy with Google:

locations = ["Utica"]
from geopy.geocoders import GoogleV3
geolocator = GoogleV3(api_key=GoogleAPIKey)
for location in locations:
    print location
    results = geolocator.geocode(query=location, language='en', exactly_one=False, timeout=5)
        for result in results:
            print result.raw

Solution

  • I'm afraid there is no way to get all possible geo-features with certain name from Google database executing a web service requests.

    Please note that the purpose of Geocoding API is resolve the address string to its coordinates. Typically Geocoding API web service will provide only one result that is the best match for the address string according to Google criteria (prominence, internal scoring, etc.). If your search string is ambiguous like the 'Utica', the Geocoding web service will choose the most prominent result that is apparently 'Utica, NY, USA'.

    As a workaround of your problem you can try to use the Places API autocomplete requests. The autocomplete requests will return up to 5 suggestions for your search address string. Unfortunately, it doesn't guarantee all possible geo-features, but you will have at least 5 of them.

    Have a look at the following request that searches localities with name 'Utica' within the USA:

    https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Utica&types=(cities)&components=country%3AUS&key=YOUR_API_KEY

    This request returns following predictions:

    • Utica, NY, USA (place ID ChIJKXZqNVE32YkRhvztGCY2GhE)
    • Utica, MI, USA (place ID ChIJz8RnUrndJIgRNUbzGmaEdTk)
    • Utica, OH, USA (place ID ChIJ1ajvLWQwOIgR6Axda9Ip9XA)
    • Utica, IL, USA (place ID ChIJdeJyBUlZCYgRpPiIgT2O7gE)
    • Utica, KY, USA (place ID ChIJzbKUniCWb4gRj_IDgwlk-N0)

    Finally, as you are using Python I would suggest using the Python client library for Google Maps API Web Services in order to execute places autocomplete requests:

    https://github.com/googlemaps/google-maps-services-python

    The documentation for this client library is available at:

    https://googlemaps.github.io/google-maps-services-python/docs/

    I hope this helps!