# Distance Between Locations Error (Spherical Law of Cosines)

**URL:** <https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825>\
**Category:** Questions about Thunkable X\
**Tags:** solved\
**Created:** [April 6, 2021, 12:54pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825 "2021-04-06T12:54:06Z")\
**Posts on this page:** 10\
**Page:** 1

<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, 12:54pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825/1 "2021-04-06T12:54:06Z")

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

---

<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:** [April 6, 2021, 4:53pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825/2 "2021-04-06T16:53:29Z")

</div>

you got your lat long assignments reversed:  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/d/5d581e5fe834690f2367e3c3b40d8c7d5c002363.png)

you can use this link to verify your calculations:  
[https://andrew.hedges.name/experiments/haversine/](https://andrew.hedges.name/experiments/haversine/)

---

<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, 5:56pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825/3 "2021-04-06T17:56:07Z")

</div>

I made a mistake before where I entered the properties backwards in the locations list, so I thought I could reverse them in the main code. Just in case, I went back and edited it so that it was all correct. It still does not work. Here is the updated project:

[New Project Link](https://x.thunkable.com/copy/d9b4a9de56964fcfebfbd40598e4c7f8)

---

<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:** [April 6, 2021, 6:16pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825/4 "2021-04-06T18:16:42Z")

</div>

You probably need some delays. You’re trying to do things **very fast**. For example, you have a complex distance calculation inside of a variable declaration inside of a loop. You’re also calling the location sensor twice when you only need to call it once to assign values to _lat1_ and _lon1_.

I would add some delays and watch to see what happens. You can set labels to variable values before each wait block to verify that the values you think are being calculated are correct.

I’ve done this exact thing so I know it’s possible.

---

<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, 6:54pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825/6 "2021-04-06T18:54:28Z")

</div>

I think the delay is helping, but i’m not sure how. There are 67 items in the locations list, and the number should go until 67, but it stops at 2. Does anyone know why or how I could solve this?  
[Updated Project](https://x.thunkable.com/copy/f21209eabebada89af739503a19be5e7)

---

<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:** [April 6, 2021, 9:08pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825/7 "2021-04-06T21:08:29Z")

</div>

I modified the project to just display lat/lon pairs for each pre-set location you have. And then calculated the distance from my location sensor (by the way, your code will be more efficient if you set the value of a variable to your calculate distance function rather than calling it multiple times for the same location).

For me, each location after the first (Garrett County Health Department) is farther than the previous location. So it appears it’s working correctly by stopping at the first location in the list. I’m guessing you’re not closest to that location when you test it…?

Here’s the debugging version I used:

[https://x.thunkable.com/copy/2565118421ee0ddf348f01bdffa01ad0](https://x.thunkable.com/copy/2565118421ee0ddf348f01bdffa01ad0)

---

<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, 9:35pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825/8 "2021-04-06T21:35:26Z")

</div>

Thank you so much! The program works now and it shows the closest location me. I have spent a lot of time trying to find the error, and this really helps! 😃

---

<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:** [April 6, 2021, 9:40pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825/9 "2021-04-06T21:40:44Z")

</div>

i’ve modified it to add some delays but you have to clean up.

1. i added a label1 for debugging (‘ok’ if good - you can simply display nothing)
2. since all your entries in the directory have reverersed lat longs, i also reversed the assignment - you should redo your directory and change back the code.
3. you had 2 blocks ‘when automode starts’ so i merged them  
try it out  
[Thunkable](https://x.thunkable.com/copy/59de83db4b07b5d13fffe7cbe44f1de2)

it’s a nice app!

---

<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 7, 2021, 1:35pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825/10 "2021-04-07T13:35:21Z")

</div>

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)\
**Post date:** [November 8, 2024, 1:25pm UTC](https://community.thunkable.com/t/distance-between-locations-error-spherical-law-of-cosines/1208825/11 "2024-11-08T13:25:29Z")

</div>


