# Google directions API and lists

**URL:** <https://community.thunkable.com/t/google-directions-api-and-lists/996664>\
**Category:** Questions about Thunkable X\
**Created:** [November 24, 2020, 5:57pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664 "2020-11-24T17:57:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rwplummer1yf4f](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rwplummer1yf4f](https://community.thunkable.com/u/rwplummer1yf4f)\
**Post date:** [November 24, 2020, 5:57pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/1 "2020-11-24T17:57:26Z")

</div>

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](https://community.thunkable.com/uploads/short-url/qkZbVaInsH5PQ4jLHzFtch1jBws.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.

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/e/a/ea37ec8d9edf014b169bd4cb2c731e652d920db3.png)

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 24, 2020, 6:52pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/2 "2020-11-24T18:52:28Z")

</div>

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

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

 ![Screen Shot 2020-11-24 at 11.02.38 AM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/b/e/be656dd7025d066356d3011c0b22f6275aabbc72.png)

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.

---

<div class="post-metadata">

**Author:** ![rwplummer1yf4f](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rwplummer1yf4f](https://community.thunkable.com/u/rwplummer1yf4f)\
**Post date:** [November 24, 2020, 7:46pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/3 "2020-11-24T19:46:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 24, 2020, 7:57pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/4 "2020-11-24T19:57:20Z")

</div>

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):

 ![Screen Shot 2020-11-24 at 11.55.49 AM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/6/0/60c10a2af2f299dd3d7ef4e071b21aabe5066539.png)

---

<div class="post-metadata">

**Author:** ![rwplummer1yf4f](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rwplummer1yf4f](https://community.thunkable.com/u/rwplummer1yf4f)\
**Post date:** [November 24, 2020, 8:00pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/5 "2020-11-24T20:00:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 24, 2020, 8:01pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/6 "2020-11-24T20:01:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 24, 2020, 8:11pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/7 "2020-11-24T20:11:04Z")

</div>

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

> **[Parsing Google Distance API JSON data.webm (video)](https://drive.google.com/file/d/1URQtZqYda5tf4VPk4Z97PKtW-bVV7k1O/view)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![rwplummer1yf4f](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rwplummer1yf4f](https://community.thunkable.com/u/rwplummer1yf4f)\
**Post date:** [November 25, 2020, 6:33pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/8 "2020-11-25T18:33:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)\
**Post date:** [November 25, 2020, 8:02pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/9 "2020-11-25T20:02:38Z")

</div>

For each loop, or use the length of list block

---

<div class="post-metadata">

**Author:** ![rwplummer1yf4f](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rwplummer1yf4f](https://community.thunkable.com/u/rwplummer1yf4f)\
**Post date:** [November 25, 2020, 8:06pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/10 "2020-11-25T20:06:28Z")

</div>

Thanks for that. Sounds like a good idea.

CHeers

Bob

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 25, 2020, 9:41pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/11 "2020-11-25T21:41:20Z")

</div>

Sounds like a very useful app!

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

 ![Screen Shot 2020-11-25 at 1.39.45 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/5/25e792eb347bba58d8b85ff68e55af355f25c1de.png)

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.

---

<div class="post-metadata">

**Author:** ![rwplummer1yf4f](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rwplummer1yf4f](https://community.thunkable.com/u/rwplummer1yf4f)\
**Post date:** [November 26, 2020, 9:06pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/12 "2020-11-26T21:06:35Z")

</div>

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.

 ![GoogleDirections](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/3/5/3504e2736b2dadd89e0cadab0cc076a8e6775551.png)

I have also included a more detailed directions output file as this is what I am using as my example file.[JSONDIrections2.txt](https://community.thunkable.com/uploads/short-url/9sdJl60lFOlLOlC1oVZ2lE9oCyq.txt) (22.5 KB)

Thanks in advance.  
Bob

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 26, 2020, 9:29pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/13 "2020-11-26T21:29:41Z")

</div>

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

 ![Screen Shot 2020-11-26 at 1.28.54 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/d/d/dd5a65a5cafa88af7343b91fb6caaebe9d9156bf.png)

---

<div class="post-metadata">

**Author:** ![rwplummer1yf4f](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rwplummer1yf4f](https://community.thunkable.com/u/rwplummer1yf4f)\
**Post date:** [November 26, 2020, 9:52pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/14 "2020-11-26T21:52:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 26, 2020, 9:57pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/15 "2020-11-26T21:57:44Z")

</div>

You’re welcome!

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

---

<div class="post-metadata">

**Author:** ![luv.ak.tech](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/luv.ak.tech/32/104896_2.png) [@luv.ak.tech](https://community.thunkable.com/u/luv.ak.tech)\
**Post date:** [November 27, 2020, 3:32am UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/16 "2020-11-27T03:32:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rwplummer1yf4f](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rwplummer1yf4f](https://community.thunkable.com/u/rwplummer1yf4f)\
**Post date:** [November 27, 2020, 10:42am UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/17 "2020-11-27T10:42:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![luv.ak.tech](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/luv.ak.tech/32/104896_2.png) [@luv.ak.tech](https://community.thunkable.com/u/luv.ak.tech)\
**Post date:** [November 27, 2020, 11:38am UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/18 "2020-11-27T11:38:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)\
**Post date:** [November 27, 2020, 1:07pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/19 "2020-11-27T13:07:34Z")

</div>

@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](http://book.mixu.net/node/ch5.html), it was a big help for me.

---

<div class="post-metadata">

**Author:** ![rwplummer1yf4f](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@rwplummer1yf4f](https://community.thunkable.com/u/rwplummer1yf4f)\
**Post date:** [November 27, 2020, 3:01pm UTC](https://community.thunkable.com/t/google-directions-api-and-lists/996664/20 "2020-11-27T15:01:50Z")

</div>

Thanks for the steer. Very useful article.

[Next page](https://community.thunkable.com/t/google-directions-api-and-lists/996664.md?page=2)
