0

I have a list of tuples that I want to iterate through and send a http request via the python requests module.

api_schedule = [
{'destination': (51.490527, 0.267840), 'id': 'trial_a', 'origin': (51.430732, 0.239308)},
{'destination': (51.429488, 0.239060), 'id': 'trial_b', 'origin': (51.490518, 0.267578)}]

The origin and destination coordinates are proving difficult to format correctly. The Google Directions API expects them in the format lat,lng where the lat and lng are float

I iterate through the list of dictionaries like so:

for pair in api_schedule:
    departure_time = time.time()
    origin_lat = str(pair['origin'][0])
    origin_lng = "," + str(pair['origin'][1])
    origin = origin_lat + origin_lng
    destination_lat = str(pair['destination'][0])
    destination_lng = "," + str(pair['destination'][1])
    destination = destination_lat + destination_lng
    params = {
    "origin" : origin,
    "destination" : destination
    "key" : api_Key,
    "departure_time" : departure_time,
    "mode" : mode
    }
    request = requests.Request('GET', url_raw, params=params).prepare()
    print request.url

However, this returns %2C in place of ,:

https://maps.googleapis.com/maps/api/directions/json?origin=51.430732%2C0.239308&destination=51.490527&destination=0.26784

How do I iterate through a list of tuples like this and use requests to formula the parameters correctly?

When I try without the above manipulation, requests views the origin and destination data as lists and thus creates two params like so:

https://maps.googleapis.com/maps/api/directions/json?origin=51.430732&origin=0.239308

Desired output:

https://maps.googleapis.com/maps/api/directions/json?origin=51.430732,0.239308&destination=51.490527,0.26784
5
  • What would be the required result? Commented Dec 5, 2016 at 9:48
  • The first question helped me understand why it wasn't working. The field is being converted to a list. This question is how do I solve this problem for this application. Commented Dec 5, 2016 at 9:49
  • Please define ' formula the parameters correctly': What is the expected URL? Commented Dec 5, 2016 at 9:50
  • I have added the desired output. https://maps.googleapis.com/maps/api/directions/json?origin=51.430732,0.239308&destination=51.490527,0.26784 instead of https://maps.googleapis.com/maps/api/directions/json?origin=51.430732&origin=0.239308 or https://maps.googleapis.com/maps/api/directions/json?origin=51.430732%2C0.239308&destination=51.490527&destination=0.26784 Commented Dec 5, 2016 at 9:52
  • 1
    %2C is entirely correct and expected in a URL. Google will decode that for you. If you were to use your 'desired output' URL in a browser, your browser would also encode the comma. Commented Dec 5, 2016 at 9:59

1 Answer 1

1

https://maps.googleapis.com/maps/api/directions/json?origin=51.430732%2C0.239308&destination=51.490527,0.26784 and https://maps.googleapis.com/maps/api/directions/json?origin=51.430732,0.239308&destination=51.490527,0.26784 are identical.

It is correct to use %2C instead of , in an URL. Your preparation of params is OK.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.