''' DS2000 Spring 2020 Source code from class - Google API Two example functions -- distancematrix, and geocode For each one, we break down the return JSON data so we can isolate the values we care about. ''' import requests # Your API key would go here API_KEY = '' def get_distance(api_key, orig, dest): ''' Function: get_distance Parameters: API key (a string) returns: dictionary of JSON data ''' http_params = {'key' : api_key, 'origins' : orig, 'destinations' : dest} url = 'https://maps.googleapis.com/maps/api/distancematrix/json' response = requests.get(url, params = http_params) return response.json() def get_geocode(api_key, address): ''' Function: get_geocode Parameters: API key (a string- Returns: dictionary of JSON data ''' http_params = {'key' : api_key, 'address' : address} url = 'https://maps.googleapis.com/maps/api/geocode/json' response = requests.get(url, params = http_params) return response.json() def get_coordinates(geo_data): ''' Function: get_coordinates Parameters: dictionary of geocode JSON data returns: tuples of lat/long (floats) ''' coords = geo_data['results'][0]['geometry']['location'] lat = coords['lat'] long = coords['lng'] return (lat, long) def main(): distance_results = get_distance(API_KEY, 'Boston, MA', 'New York, NY') duration = distance_results['rows'][0]['elements'][0]['duration']['text'] print('To get to new york it takes...', duration) geocode = get_geocode(API_KEY, '440 Huntington Ave. Boston MA 02115') lat, long = get_coordinates(geocode) print('WVH is at', lat, long) main()