Hi - I’m tearing my hair out with a problem and was wondering if anyone here might be able to help.
I have an Airtable spreadsheet that looks like this:
Name|Latitude|Longitude
Bao|51.513256|-0.136554
El Pastor|51.505315|-0.091193
Xu|51.511144|-0.132614
I’m trying to create and display a list which shows the distance from an inputted location to each of these restaurants, and the name of each restaurant. I’m doing this using the Google Maps API.
The output I get from this in the app is a list of the correct distances to each restaurant, but it just repeats the name of the last restaurant 3 times. e.g.:
Xu 3248seconds
Xu 177seconds
Xu 2913seconds
The distances are correct, and displayed in the correct order. I just can’t figure out how to display the correct restaurant name for each distance! These are my blocks - if anyone can figure out what’s wrong here, that would be absolutely amazing. Thanks!
the green Web_API block is using the Google Maps, using the coordinates of each restaurant from the spreadsheet
the purple API block gets the response; and then saves as a list the time in seconds to each restaurant (the big pink thing) and the name of the restaurant (this latter bit is what’s broken).
The problem is that the Web_API.Get uses slow asynchronous call. A cycle has worked, as Web_API.Get have not had time for it and, therefore, displayed the latest reading. You will need to modify the algorithm so that the next call to Web_API.Get happened only after the completion of the previous call.
Here it is necessary to use recursion, as Mark showed.
On which screen and in which block is the code that does not work correctly? There are a lot of duplicate blocks in your code, an incomprehensible delay of 10 seconds, a lot of unnecessary variables, no checks for errors when getting data from the API. Try to simplify the code. The more blocks - the more mistakes.