# Location sensor for tracking distance

**URL:** https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828
**Category:** Questions about Thunkable X
**Created:** [May 3, 2024, 10:43pm UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828 "2024-05-03T22:43:26Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![theyloack](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@theyloack](https://community.thunkable.com/u/theyloack)
#### Post date: [May 3, 2024, 10:43pm UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828/1 "2024-05-03T22:43:26Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/f/2/f2db5ac3c7ea2883ddc55e47cc34b06e98a81c25.png)  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/6/f/6f48751e0327f6c8bb8cc1a07fb4be30dca2fb5e.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: [May 3, 2024, 11:49pm UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828/2 "2024-05-03T23:49:49Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![theyloack](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@theyloack](https://community.thunkable.com/u/theyloack)
#### Post date: [May 4, 2024, 1:12am UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828/3 "2024-05-04T01:12:56Z")

</div>

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

Here is my app:  
[https://x.thunkable.com/copy/dd5263e2ccc64debb45a9bfdde491502](https://x.thunkable.com/copy/dd5263e2ccc64debb45a9bfdde491502)

---

<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 4, 2024, 3:02am UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828/4 "2024-05-04T03:02:23Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![theyloack](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@theyloack](https://community.thunkable.com/u/theyloack)
#### Post date: [May 4, 2024, 4:21am UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828/5 "2024-05-04T04:21:57Z")

</div>

Yes, all of the code is inside the “then do”

---

<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: [May 4, 2024, 5:04am UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828/6 "2024-05-04T05:04:37Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/3/833856e0496f393f6b987ac9111639961fff0e70.png)

I’d also base your equations off of the screenshot in this post ([Calculate distance travelled - #2 by manyone](https://community.thunkable.com/t/calculate-distance-travelled/565278/2)

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](https://community.thunkable.com/t/how-to-ask-great-questions-v2-0/2689983) ✨ [Debugging A Project](https://community.thunkable.com/t/debugging-in-thunkable-x-video/849471) ✨ [API JSON Tutorial](https://community.thunkable.com/t/api-json-tutorial-video/1140575)

Want to hire a Certified Thunkable Expert? [Elevate your app with Tatiang on Fiverr](https://www.fiverr.com/s/726B4a)

---

<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 4, 2024, 6:21am UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828/7 "2024-05-04T06:21:26Z")

</div>

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:

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

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!

---

<div class="post-metadata">

### Author: ![theyloack](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@theyloack](https://community.thunkable.com/u/theyloack)
#### Post date: [May 5, 2024, 2:08pm UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828/8 "2024-05-05T14:08:29Z")

</div>

I tried the same way on the post ( ([Calculate distance traveled - #2 by manyone](https://community.thunkable.com/t/calculate-distance-travelled/565278/2))

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?

---

<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 5, 2024, 3:50pm UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828/9 "2024-05-05T15:50:50Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)
#### Post date: [August 3, 2024, 3:51pm UTC](https://community.thunkable.com/t/location-sensor-for-tracking-distance/2983828/10 "2024-08-03T15:51:19Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
