Google directions API and lists

Hi. I am building two apps. One is for the customer to use and the other is for the taxi driver. I use the location sensor in the customer app to get his latitude and longitude and send this off to Google geocoding to get an address a driver will recognise. The customer then inputs his destination and I send this off to google again. I use google distance matrix to get the distance between two the two points and calculate a cost based on that. The driver is alerted to a new job being available when the customer submits it to the firebase RT Database. I use a listener for this.

I am a long way off finishing this but I can give you more detail on how I have done what I have done so far if you let me know what you need.

hello
i am veraat
can you help me in making a chatbot in thunkable
please.

:smiley::smiley::smiley:

welcome to our community @guptasgurgaond
why did you post this question in this topic create a new topic to show your question

because i do not now how to

sorry

go to home page of thunkable community and see at right side there will be a button called New Topic and fill the informations

Hi again. Having trouble with looping through the list still. Is j just a random variable name used for counting or does it need to refer to an item in the list? Do I need to call the variable ā€œstepsā€ or something in the list? I have these blocks and they are crashing the app. If I remove the ā€œfor each itemā€ loop the code works correctly and returns some details from the list.

Hope you can help as before.

Bob

1 Like

(@tatiang hereā€¦)

You use the j variable from the Variables drawer since thatā€™s the loop index variable. Instead of a 1 where you have a list item, you use j in its place.

Iā€™m on my phone so I canā€™t show you blocks right now but can post some later.

1 Like

Iā€™m not all that familiar with Google Directions but it seems like the API is returning a JSON response that formats the ā€œstepsā€ (within a ā€œlegā€ within a ā€œrouteā€) as, in order:

end_location
start_location
end_location
start_location
ā€¦

So if we get the first start_location and end_location, weā€™ll know where to send your drivers. Butā€¦ if we then get the next pair, we have duplicate data points because the end_location for the first step is the start_location for the second step. We really only need the first start_location and then every end_location after that.

That of course depends on how you plan to display steps data to the user/driver. Maybe you want it to show ā€œgo from lat/lng A to lat/lng Bā€ then ā€œgo from lat/lng B to lat/lng Cā€ā€¦ OR maybe you want it to show ā€œgo from lat/lng A to lat/lng Bā€ then ā€œgo to lat/lgn Cā€.

I thought I had a handle on this and started making a demo for the latter option but itā€™s not working for me. Something to do with object vs. list handling.

1 Like

Thanks for looking at this for me. I think I am going for one start location and then every end location after that. I think thats all I need to draw the polyline. Your blocks have given me some ideas which I will work on this week. Iā€™ll let you know how I get on

Cheers
Bob

Im beginning to see what you mean. This set of blocks seems to work and returns a end location result


But if I wrap it up in a loop I get no results at all. See loop blocks below.

Am I doing something wrong with the loop or placing the j in the wrong place.

I noted in your approach you created a list of the legs and steps and used the for each loop on that. Did that work and why doesnt my approach? Any ideas on where I am going wrong would be appreciated.

Cheers

Bob

The issue is that you have the ā€œjā€ variable in the right place as a list index for the ā€œstepsā€ property. But you are declaring j as an item in list ā€œapp JsonDirectionsTextā€ and my guess is that youā€™re still returning the entire JSON response as a value for ā€œapp JsonDirectionsText.ā€ Thatā€™s not going to work. You want the ā€œin list [ ]ā€ variable to be declared as everything inside of the the ā€œin list [ ] get #j block.ā€ Thatā€™s what I was trying to do with my example above but I got stuck somewhere.

I know, Iā€™m not explaining this very well.

1 Like

You are explaining it very well. Thank you.

Cheers

Bob

Its not the most elegant solution but this does seem to work. I am using the count with i function as I know the number of steps in each leg of the route. Hopefully it will be easy to create a list from the text string I am building. Iā€™ll mark this as solved but I am sure I will be back later. :slight_smile:

That looks good to me!

HI again. Trust you are keeping well in these difficult times. Iā€™ve been making good progress on my taxi app but seem to be stuck on something very simple. I want to get the duration of the first leg of the journey. Looking at the returned JSON file the following should get me what I need.


There is only one route so #1 for that. I want the first leg so #1 for that as well. Then duration then text. But this does not appear to return a value at all and I end up with a blank space in my message where the duration like ā€œ1 minā€ should be. Am I missing something here? I have attached a recent response file in case it helps.geocodedJSON.txt (7.3 KB)

That looks correct. I was able to copy and paste the JSON text from your attachment and get back ā€œ1 minā€ using these blocks:

Is your API ā€œGetā€ call returning an error (green error block)?

Are you doing anything else with the DurationStep1 variable after this that might affect its value? What happens if you replace the ā€œset app variable DurationStep1ā€ block with a ā€œfrom Label1 set Text toā€ block?

Are you initializing the DurationStep1 variable as blank (no connected block) or as a particular type (text string, list, etc.)?

I do a lot with the information return in the request and everything else works fine so I donā€™t believe I am getting an error. Here is a small sample from the main app.

. Iā€™ll try the label text idea and see what happens. Duration is currently initialised as a blank string. Iā€™m going to change that to ABC

Officially weird! I initialised the Step1Duration value to ABC and thats the value I show when I print the variable value. However when I copy the value to a label I get a value of 1 min.

Code below.

No idea why that is but I have some ideas on how to work with the issue. Thanks for giving me some ideas to move forward with.

Cheers

Bob

1 Like

can you share your app after it is finished please and can you tell are you making this on your own

Hi, I am building this on my own. Not sure about sharing it when its done as I am developing it for a customer. When I am done, it will be his property. But I can ask him.