fIwJlxSzApHEZIl
fIwJlxSzApHEZIl

Reputation: 13270

How to use the Google Places Python API to look up a place by name

How can I use the Google Maps Python API to look up a place by its name?

Digging through the tests took me a little bit as well as decoding the fields values so I figured I'd post my working example after I got it running.

More reading:

Upvotes: 3

Views: 2324

Answers (1)

fIwJlxSzApHEZIl
fIwJlxSzApHEZIl

Reputation: 13270

from environs import Env
import googlemaps
maps_api = googlemaps.Client(key=env("PLACES_API_KEY"))

place_search = maps_api.find_place(
            "St. Louis Lambert International Airport",
            "textquery",
            fields=["formatted_address", "place_id"],
            language='en-US',
        )

print("Place search results")
pprint(place_search)

place_lookup = maps_api.place(
        place_search["candidates"][0]["place_id"], fields=["website", "place_id"])

print("\nPlace lookup results")
pprint(place_lookup)

...

/usr/bin/python3 /work/project/scripts/address-duplicate-locator/main.py
Place search results
{'candidates': [{'formatted_address': '10701 Lambert International Blvd, St. '
                                      'Louis, MO 63145, United States',
                 'place_id': 'ChIJ8YhjxbQ234cRpncwZrzNq50'}],
 'status': 'OK'}

Place lookup results
{'html_attributions': [],
 'result': {'place_id': 'ChIJ8YhjxbQ234cRpncwZrzNq50',
            'website': 'http://www.flystl.com/'},
 'status': 'OK'}

Process finished with exit code 0

Full list of things you can look up with the Places API pulled from https://github.com/googlemaps/google-maps-services-python/blob/master/googlemaps/places.py :

PLACES_FIND_FIELDS_BASIC = {"business_status",
        "formatted_address",
        "geometry",
        "geometry/location",
        "geometry/location/lat",
        "geometry/location/lng",
        "geometry/viewport",
        "geometry/viewport/northeast",
        "geometry/viewport/northeast/lat",
        "geometry/viewport/northeast/lng",
        "geometry/viewport/southwest",
        "geometry/viewport/southwest/lat",
        "geometry/viewport/southwest/lng",
        "icon",
        "name",
        "permanently_closed",
        "photos",
        "place_id",
        "plus_code",
        "types",}

Upvotes: 4

Related Questions