Calculate distance travelled

hello, its me again.

how does one calculate distance travelled over time on a map in thunkable x?
I intend on generating a notification if a certain distance is covered within x amount of time

this small app solves the haversine formula for calculating the APPROXIMATE distance between 2 points expressed in latitude and longitude degrees. currently it’s set to return miles. if you want kilometers, change the factor 3961 to 6373

https://x.thunkable.com/copy/0b2e0a3c8da1a36488f062715bf5e263

3 Likes

I think that tells you the distance between two points which will rarely be the distance travelled.

Use the mapquest api to return a distance between two points/addresses. It’s easy and free to use for low level api usage

2 Likes

I wonder what comments like this add to the conversation. Please try to provide more insightful comments that help a user get to a solution instead of telling them they are wrong.

This type of desires behavior will support the community and encourage more users to come and ask questions.

1 Like

I didn’t tell the person who asked the question they are wrong. I stated that the solution provided by someone else may not be the one they are looking for.

Try any of the solutions that have earlier been given. The truth is even if you are not moving in a straight line, the solutions offered is still what you need. All you need to do for near accurate calculation is to iterate over short distances and add distances together.

3 Likes

Anyone have any luck with the Haversine formula since May?

I’m trying to create an air quality app that will located the nearest sensor by lat/long pairs. I could use an API for that but it would get expensive really quickly (19,000 calls per location search per app install). So this seems like a free, mathematical solution to the problem.

@manyone posted a set of blocks that converts to radians at the end of the calculation instead of the start, that doesn’t seem to use app dlat at all, and that swaps long1-long2 for the long2-long1 (when calculating app dlon) that’s listed in every online formula I’ve found. But I re-created the blocks as shown and got the wrong answer for lat/long pairs I tried.

I also tried to build the Haversine formula myself in Thunkable but the distance values I’m getting are way off – and not just by a simple factor (e.g. multiply by 2, divide by 6, etc.).

@manyone Any chance you can re-share the project link that’s working for you? I tried the link above but couldn’t get it (could be because I’m using the beta Drag & Drop version of Thunkable). Thanks!

here’s my latest version you could test with - it’s not ready to publish yet because i can verify the calculation of 2 sets of points using my version and haversine (in excel) but it fails when i try to work on your set of points! (38.109192,-122.578262) and (40.7128,74.006)
did you mean LON2 to be -74.006?
anyway it’s not really haversine but a replacement formula using spherical law of cosines. the results are almost the same give or take a mile.
https://x.thunkable.com/copy/6328b32ba5a3a086f91a88022955d4bd

HAVERSINE ALTERNATIVE USING SPHERICAL LAW OF COSINES

it has come to my attention that i had mistakenly posted a thunkable solution for calculating distance between 2 points on the globe as the haversine formula (i even mispelled it as havesine!). i went back to my sources and discovered that what i had posted was a simpler way of computing the same distance using the spherical laws of cosines. the results may be off by a mile or so when compared to the haversine formula, but they’re mostly pretty close.

in fact, it can be expressed as a oneliner (without using a square function, or square root, or atan that are present in haversine):

D=ACOS(SIN(LAT1)*SIN(LAT2)+COS(LAT1)*COS(LAT2)*COS(LON2-LON1))*3961

where 3961 is the radius of the earth in miles (use the equivalent 6373 in kilometers). Also note that the difference between the LAT values is not part of the formula.

when i was playing with this in may i also noticed that the trigonometric functions in thunkable rely on angles being expressed in degrees. hence where you expect radians, thunkable actually needs degrees. it’s an important thing to remember.

here’s the link to the new version (remember it’s a haversine replacement!):
https://x.thunkable.com/copy/0a769291e941b464b63f836aa39930e1

if anyone is interested, i’ve included link to an exerciser spreadsheet. just enter the lat/ long points at top and get the result in cell marked DISTANCE

3 Likes

Aha! Yes, it should be -74.006. Thank you for catching that. I was trying to find a spot in New York and my Google search returned 74.006 W but I didn’t realize that needed to be negative.

That explains a lot. Sorry, very new to this!

1 Like

I tried your second Thunkable project with the locations I’m using for testing: one (relatively) near San Francisco and one in New York City. The distance returned was ~2565 which is what I expected. Awesome!

It turns out that an excel formula I had set up in a Google sheet (looks to be the same as you posted) also returns the same value as your project so I feel pretty good about that. That negative longitude was really throwing me off!

I appreciate your help so much! Great to have the examples and the details of how you went about calculating distances.

2 Likes

Why can’t I make that work?

I did a project here:
https://x.thunkable.com/copy/ab7f391eae78b2a60232b46aa4ce76de
that does what’s in the guide, but still it doesn’t work?


Working in km by the way…
I get 7365 km, where you get 347…

Are you looking for something like this? Much easier when done server side. Particularly when you start dealing with bigger data sets

1 Like

Thank you Jared, but I’m trying to do the calculations in-app - that should be possible, shouldn’t it?

Not counting on more than 10 points in a list.

Definitely should be possible! You got this!

@engberg Here’s the fix:

When you are setting the value of app d, you need to take the acos of w+v. But you are taking the acos of w and then adding v to the result.

So instead of acos(w)+v, it needs to be acos(w+v). If you fix that, then you get 347.

2 Likes

I could not see the forest for trees - thank you @tatiang for giving it a second look…

Hello,
Me and a few of my friends are trying to create an app that finds the closest location to your current one. The locations are a list of objects with properties of latitude, longitude, and location name. I found a post that tried to do a similar thing with the spherical law of cosines, but it won’t work when I try and implement it with my code. Is anyone able to find an error with my code or has an idea on how to fix it? I want to continue to use the locations list that I have already implemented if possible because there a lot of locations I am working with.

The location that I keep on receiving is shown below, but there are much closer locations in the list.
image

My Project Link
Referenced Post Link

1 Like