Call Google Maps Geocoding Api in Google Cloud Functions (Python)

April 21, 2019

Enable Google Maps Platform

Get API Keys

NOTE: Make sure Geocoding API is enabled.

requirements.txt

# https://pypi.org/project/googlemaps/
googlemaps==3.0.2

main.py

def test_geocoding(request):
    import googlemaps
    from flask import jsonify

    # 40.714224, -73.961452
    location = request.json.get('location')
    if location:
        location = tuple(location.split(', '))

    if not location:
        abort(422)


    API_KEY = 'AIza ...'
    gmaps = googlemaps.Client(key=API_KEY)
    result = gmaps.reverse_geocode(location)

    return jsonify(result)

Test

curl -X POST [URL]/test_geocoding -H "Content-Type:application/json" -d '{"location":"40.714224, -73.961452"}'

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.