Location sensor for tracking distance

The haversine formula works correctly when I hardcode the latitude and longitude, but when I try to get them from the location sensor, my output is always 0.


What are the values of app variable last_lat and app variable last_lon right before you call the function to calculate the distance? What are the values of the green latitude and longitude blocks after calling the GetCurrentLocation function?

You can display them in a text inputโ€™s text (to be able to copy them) or a labelโ€™s text. I would get those values and try all four manually in the formula1 function to see if that works.

The values for the first interaction, after walk 10 seconds, are:

last_lat = 42.99577312547992
last_lon = -71.4658010937939
longitude = -71.46576614125767
latitude = 42.99580510244052
image

Here is my app:
https://x.thunkable.com/copy/dd5263e2ccc64debb45a9bfdde491502

Your first screenshot shows weird code patterns. Iโ€™m assuming that all your blocks belong in the โ€œthen doโ€ section unless they are truly unrelated to the blocks that come before them.

Yes, all of the code is inside the โ€œthen doโ€

Your โ€œdโ€ value in the formula1 function is returning zero. Iโ€™m not sure why but you need to fix that equation. Iโ€™d suggest starting from scratch with that line of code (not the whole formula, just the calculation for โ€œdโ€).

Iโ€™d also base your equations off of the screenshot in this post (Calculate distance travelled - #2 by manyone

Little changes youโ€™ve made such as multiplying app variable d by a constant later instead of as part of the calculation of app variable d can have impacts that you donโ€™t expect.

Also a note that Iโ€™ve used the Haversine formula to calculate relatively large distances like 0.5 miles but Iโ€™ve never used it for distances of just a few feet. I donโ€™t know if itโ€™s accurate at such small scale.

ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท
Need help? How to Ask Great Questions :sparkles: Debugging A Project :sparkles: API JSON Tutorial

Want to hire a Certified Thunkable Expert? Elevate your app with Tatiang on Fiverr

last_lat = 42.99577312547992
last_lon = -71.4658010937939
longitude = -71.46576614125767
latitude = 42.99580510244052
[/quote]

i asked Bing to calculate the distance between these 2 points and it came back with this:

the answer is about 4.5 meters - and thatโ€™s using high precision trigonometric routines.

i think you;re getting zero because the trigonomoetric routines being used by thunkable have a low precision such that the sin of a very small angle is rounded to zero. if you examine the formula, youโ€™ll end up computing the sin (squared) of a verrry small delta change between two angles.

i think you cannot rely on this formula on points that are very close!

1 Like

I tried the same way on the post ( (Calculate distance traveled - #2 by manyone )

I still get 0 for my โ€œdโ€ calculation because some of (w + v) are close to 1, and asin(1) is 0. Do you think it is possible to calculate the distance while Iโ€™m walking in thunkable?

I donโ€™t recommend it.
But you should put it to a test. Surely your waking exercise is more than 4 meters or 10 feet. Also bring one of thise step counters tto verify. Please let us know your results.

1 Like