Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having trouble with an api call in python. When I set r to a fixed address the program returns the correct json call

I am having trouble with an api call in python. When I set r to a fixed address the program returns the correct json call but when I create variables for mode, orgin and destination I get an invalid requet. I am not sure why it does not work.

code works with set api call:

___________________________________________________________________________________

import json import requests

class Tour: def __init__(self, master): self.master = master master.title("Tour") Label(master, text="Orgin").grid(row=0) Label(master, text="Destination").grid(row=1) Label(master, text="Mode").grid(row=2) e1 = Entry(master) e2 = Entry(master) e3 = Entry(master) e1.grid(row=0, column=1) e2.grid(row=1, column=1) e3.grid(row=2, column=1) Button(master, text='GetDistance', command= self.onClick()).grid(row=3, column=0, sticky=W, pady=4) def onClick(self): api_key = 'AIzaSyCTzy54Y4XxqFuxNPKVxvpnJO5iNpdofOI' source = 'Irvine CA' dest = 'Los Angeles CA' mode = 'driving' url ='https://maps.googleapis.com/maps/api/distancematrix/json?' r = requests.get('http://maps.googleapis.com/maps/api/distancematrix/json?origins=New+York+NY&destinations=Lansing+MI&mode=driving&sensor=false') x = r.json() print(x)

root = Tk()

my_gui = Tour(root)

root.mainloop()

__________________________________________________________________

Code with variables does not work:

____________________________________________________________

import json import requests

class Tour: def __init__(self, master): self.master = master master.title("Tour") Label(master, text="Orgin").grid(row=0) Label(master, text="Destination").grid(row=1) Label(master, text="Mode").grid(row=2) e1 = Entry(master) e2 = Entry(master) e3 = Entry(master) e1.grid(row=0, column=1) e2.grid(row=1, column=1) e3.grid(row=2, column=1) Button(master, text='GetDistance', command= self.onClick()).grid(row=3, column=0, sticky=W, pady=4) def onClick(self): api_key = 'AIzaSyCTzy54Y4XxqFuxNPKVxvpnJO5iNpdofOI' source = 'Irvine CA' dest = 'Los Angeles CA' mode = 'driving' url ='https://maps.googleapis.com/maps/api/distancematrix/json?' r = requests.get(url + 'origins =' + source + '&destinations= ' + dest + '&mode=' + mode + '&sensor=false') x = r.json() print(x)

root = Tk()

my_gui = Tour(root)

root.mainloop()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

Explain the different types of marketing strategies.

Answered: 1 week ago