Google directions API and lists

I would appreciate some help with a couple of issues I am stuck on. I have managed to get a response from the Google directions API where I Have a start location, a way point and a destination. I want to use the lat and lng values returned to plot a polylione on a map.

My problems are, I dont know how to parse the response to get all the lat and lng values returned. I can see the “path” through the response is routes - legs - steps but there are two steps sections and I dont know how to include both. I also confess I dont really understand how lists work and I have been trying to show my results in a list viewer just to prove I Have correctly parsed the returned file.

I have attached the sample google response file for your information.JSONDIrections.txt (8.9 KB)

Can anyone tell me how I parse the file to get all the latitude and longitude records in order from both steps sections of the attached file.

My failing blocks are included here.

1 Like

Kudos for including a screenshot and the actual JSON response text. :slight_smile:

I pasted your JSON text into https://jsonformatter-online.com so I could understand it better (click to view larger version):

It looks like you’ll want to get the property “routes” from “JSonDirectionsText” (the JSON object), then take the first list item from that, then take the legs property from that, then take the first list item from that, then take the end_location property from that, and finally, take the lat (or lng) property from that. Phew!

Each bracket [ is the start of a list so when you see that, you know you have to get the first (or sometimes a different index) list item.

I can work up some blocks later if you need them but try that and let me know how it works…

I would suggest trying to get a single value before attempting a loop.

2 Likes

Hi, Thanks for looking into this for me. I would appreciate some blocks if you can as I have been looking at this for a long while and confess to being completely stumped.

Cheers

Bob

1 Like

Understanding how to read and parse JSON took me a loooong time. And I’m still not an expert.

Here’s what I would try (click to view larger version):

2 Likes

Ah I see. I had the properties the wrong way round. I’ll have a play with that over the next day or so and see how I get on. Thanks.

1 Like

You’re welcome. You should get back 51.7068738. And I was able to copy the JSON text into a variable and test it and verified that it did display 51.7068738.

1 Like

Here’s a video where I explain how to parse what you’re seeing from the JSON result:

2 Likes

Wow that is awesome. Thank you for taking the trouble to explain it to me. What I am doing is writing a sort of Uber app for a friend of mine. I have a customer app and a driver app. The customer requests a taxi from his current location to a destination of his choice. With the driver app the driver can choose to accept the job or decline it. This all works fine so far. What I then want to do, when the driver accepts the job is to show a map to the driver using the driver location as the start point, the customers location as the way point and the customer destination as the final point. During testing i play the part of both people whihc is why the first location and the way point are both at highlands park. I think if I drive out with my iphone and request a taxi and then return home and accept the job as the driver at my home I will get a response file in the same format that make more sense as well. Thought you might like the background.

This is going to take me a while to work through but can I ask for one more piece of advice. How would you loop through the file as the number of step by step directions will change for every route requested. I dont like the idea of using an artificially high loop value as I just see that causing an error when it gets to the end of the file. Is there a DO While NOT End Of File or equivalent in Thunkable that you know of?

Thanks again for the trouble you have taken to help me.

Cheers

Bob

1 Like

For each loop, or use the length of list block

2 Likes

Thanks for that. Sounds like a good idea.

CHeers

Bob

1 Like

Sounds like a very useful app!

Yep, these two loop blocks are the way to go:

I really like the “for each item j in list” block but if you need to know which index # it lands on at some point, then you have to manually increase a counter variable value by 1 each iteration of the loop. The “count with i” block does that for you since you can use “i” as the index value.

1 Like

Ive started to stumble a little sooner than I had hoped. I have the starting latitude and longitude ok and I can now show those in the app as detailed in the screenshot. What I think would be useful is a count of the items in the steps list. Using JsonFormatter as you advised I can see the first steps list contains 7 items but the Json provided by Google doesn’t have this information. If I run the blocks as shown here I get a steps=0 result. I presume JsonFormatter works this out somehow. Do you know how I might be able to count this.

I have also included a more detailed directions output file as this is what I am using as my example file.JSONDIrections2.txt (22.5 KB)

Thanks in advance.
Bob

1 Like

Here you go. You were just missing the “length of [list]” block from the Lists drawer:

2 Likes

Awesome. I confess to having a mental block on lists. Thanks again.

1 Like

You’re welcome!

Well, you’ve done a deep dive into JSON and lists at the same time… not easy stuff.

1 Like

sorry to disturb but is this topic about making way in a map?

Yep. Thats it exactly. I am trying to draw a polyline on a google map.

can you tell me how to do it i thought to make it but then i thought it cant happen but now i saw this topic can you please explain me because i did not got how to use it as you said

@rwplummer1yf4f,

I’m glad @tatiang was able to help you solve this problem.

If you are going to be working with JSON and/or Web APIs, you might check out this article on JSON, it was a big help for me.

1 Like

Thanks for the steer. Very useful article.