# Calculate distance travelled

**URL:** <https://community.thunkable.com/t/calculate-distance-travelled/565278>\
**Category:** Questions about Thunkable X\
**Created:** [May 11, 2020, 8:23am UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278 "2020-05-11T08:23:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anusdesignstudent](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/anusdesignstudent/32/68752_2.png) [@anusdesignstudent](https://community.thunkable.com/u/anusdesignstudent)\
**Post date:** [May 11, 2020, 8:23am UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/1 "2020-05-11T08:23:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![manyone](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@manyone](https://community.thunkable.com/u/manyone)\
**Post date:** [May 13, 2020, 7:56am UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/2 "2020-05-13T07:56:56Z")

</div>

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](https://x.thunkable.com/copy/0b2e0a3c8da1a36488f062715bf5e263)

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/f/1/f1a5ef281b130fcdab0ed20d93430dad2361521b.png)

---

<div class="post-metadata">

**Author:** ![Dean\_Artis](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dean\_Artis](https://community.thunkable.com/u/Dean_Artis)\
**Post date:** [May 13, 2020, 1:04pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/3 "2020-05-13T13:04:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [May 13, 2020, 5:24pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/4 "2020-05-13T17:24:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [May 13, 2020, 5:26pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/5 "2020-05-13T17:26:16Z")

</div>

> [@Dean\_Artis](#):
>
> I think that tells you the distance between two points which will rarely be the distance travelled.

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.

---

<div class="post-metadata">

**Author:** ![Dean\_Artis](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Dean\_Artis](https://community.thunkable.com/u/Dean_Artis)\
**Post date:** [May 13, 2020, 8:14pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/6 "2020-05-13T20:14:22Z")

</div>

> [@jared](#):
>
> Please try to provide more insightful comments that help a user get to a solution instead of telling them they are wrong.

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.

---

<div class="post-metadata">

**Author:** ![Patrick\_Elisha](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/patrick_elisha/32/68464_2.png) [@Patrick\_Elisha](https://community.thunkable.com/u/Patrick_Elisha)\
**Post date:** [May 14, 2020, 10:12am UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/8 "2020-05-14T10:12:31Z")

</div>

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.

---

<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:** [September 7, 2020, 8:30pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/9 "2020-09-07T20:30:54Z")

</div>

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

 ![Screen Shot 2020-09-07 at 1.29.09 PM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/f/7/f7efe12b695d5653a0b1b46104f7b96c91685fcd.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:** [September 8, 2020, 12:47am UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/12 "2020-09-08T00:47:05Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![manyone](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@manyone](https://community.thunkable.com/u/manyone)\
**Post date:** [September 8, 2020, 12:59am UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/13 "2020-09-08T00:59:17Z")

</div>

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](https://x.thunkable.com/copy/6328b32ba5a3a086f91a88022955d4bd)

---

<div class="post-metadata">

**Author:** ![manyone](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@manyone](https://community.thunkable.com/u/manyone)\
**Post date:** [September 8, 2020, 2:01am UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/14 "2020-09-08T02:01:23Z")

</div>

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](https://x.thunkable.com/copy/0a769291e941b464b63f836aa39930e1)

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f2e27adfb8b330caea7f4b35a0d36831f351023.png)

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

> **[distcalc\_sphere](https://docs.google.com/spreadsheets/d/1uHVBqWjbnjroO-Z4FoCefKIfTmerCSZYCfuaLEh-_Gw/edit?usp=sharing)**
>
> Sheet1
> 
> LAT,LONG,https://www.geeksforgeeks.org/haversine-formula-to-find-distance-between-two-points-on-a-sphere/
> POINT1,38.898819,-77.03669,https://notunreasonable.com/2011/09/19/latlon-distance-formula-in-excel-haversine-and-spherical-law-of-cosine...

---

<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:** [September 8, 2020, 3:03am UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/15 "2020-09-08T03:03:49Z")

</div>

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!

---

<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:** [September 8, 2020, 3:50am UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/16 "2020-09-08T03:50:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![engberg](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/engberg/32/31802_2.png) [@engberg](https://community.thunkable.com/u/engberg)\
**Post date:** [October 4, 2020, 5:20pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/17 "2020-10-04T17:20:47Z")

</div>

Why can’t I make that work?

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

 ![Skærmbillede 2020-10-04 19.15.10](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/4/b/4b2b173b40bd559d2af15702bc30c6212cdc6f71.png)  
Working in km by the way…  
I get 7365 km, where you get 347…

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [October 4, 2020, 5:51pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/18 "2020-10-04T17:51:13Z")

</div>

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

> [@How to get the closest distance to my point](http://community.thunkable.com/t/how-to-get-the-closest-distance-to-my-point/791117/5):
>
> I’m working on the tutorial for this!!! [UPDATE] you’re looking to get a response like [this](https://api.backendless.com/34292C6A-B3A7-83DD-FF79-EF9E6913F900/FD17E678-AFB3-49F3-9663-8CF8D54CE7C7/data/thunkableLocations?where=distanceOnSphere(%27POINT(-85.73455637260194%2042.46124149689038)%27%2C%20location)%20%3C%20mi(33.48396187841322)%20&props=distanceOnSphere(%27POINT(-85.66061443893842%2042.375649975692355)%27,%20location)%20*%200.000621371%20as%20milesToPoint,city,state&sortBy=milesToPoint) back, right? i create the milesToPoint property everytime the database is called using the URL scheme above. i include only milesToPoint,state,city,objectId. everything done very quickly by the DB server. The search was conducted using arbitrary coordinates near my home! without any fancy querying the response looks like this.

---

<div class="post-metadata">

**Author:** ![engberg](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/engberg/32/31802_2.png) [@engberg](https://community.thunkable.com/u/engberg)\
**Post date:** [October 4, 2020, 6:08pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/19 "2020-10-04T18:08:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [October 4, 2020, 6:10pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/20 "2020-10-04T18:10:59Z")

</div>

Definitely should be possible! You got this!

---

<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:** [October 4, 2020, 9:52pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/21 "2020-10-04T21:52:48Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![engberg](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/engberg/32/31802_2.png) [@engberg](https://community.thunkable.com/u/engberg)\
**Post date:** [October 5, 2020, 8:40am UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/22 "2020-10-05T08:40:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aanshi6](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/aanshi6/32/101076_2.png) [@aanshi6](https://community.thunkable.com/u/aanshi6)\
**Post date:** [April 6, 2021, 3:35pm UTC](https://community.thunkable.com/t/calculate-distance-travelled/565278/23 "2021-04-06T15:35:57Z")

</div>

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](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/e/5ecbee5478fe53450ed9712a441c7625a557a57e.png)

[My Project Link](https://x.thunkable.com/copy/a1f71378ae79dc3ecdfa822d683f9fb2)  
[Referenced Post Link](https://community.thunkable.com/t/calculate-distance-travelled/565278/14)

[Next page](https://community.thunkable.com/t/calculate-distance-travelled/565278.md?page=2)
