Distance Between Locations Error (Spherical Law of Cosines)

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.

My Project Link
Referenced Post Link

you got your lat long assignments reversed:

you can use this link to verify your calculations:

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

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.

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

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:



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! :smiley:

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

it’s a nice app!

1 Like

Thank you for your help!