# \[Solved\] How do I find/add Map Markers Nearest to the User (Haversine formula)

**URL:** <https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411>\
**Category:** Maps\
**Created:** [June 6, 2020, 7:42pm UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411 "2020-06-06T19:42:13Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobsunscreen](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/bobsunscreen/32/85667_2.png) [@bobsunscreen](https://community.thunkable.com/u/bobsunscreen)\
**Post date:** [June 6, 2020, 7:42pm UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/1 "2020-06-06T19:42:13Z")

</div>

I am trying to make it so the map in my app only loads markers within a 50 mile radius of the user’s current location. (I am saving the map marker latitudes and longitudes to Airtable.) Is this possible? If so, how would I go about coding this?

---

<div class="post-metadata">

**Author:** ![actech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@actech](https://community.thunkable.com/u/actech)\
**Post date:** [June 7, 2020, 10:20am UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/2 "2020-06-07T10:20:59Z")

</div>

In the simplest case, you need to calculate the distance between two markers. if it is less than the specified radius, then output the marker.

You can also view the API for working with maps that already have a function for determining the distance between two coordinates on the map

---

<div class="post-metadata">

**Author:** ![bobsunscreen](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/bobsunscreen/32/85667_2.png) [@bobsunscreen](https://community.thunkable.com/u/bobsunscreen)\
**Post date:** [June 8, 2020, 5:08pm UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/3 "2020-06-08T17:08:58Z")

</div>

Okay, thank you! Could you direct me to a Thunkable app that utilizes a function that determines the distance between two coordinates?

---

<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 2, 2020, 1:23am UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/4 "2020-10-02T01:23:00Z")

</div>

See this app! I am working on videos and polishing the app currently but can answer questions via dm

> [@CRUD and Geolocation Example App](http://community.thunkable.com/t/crud-and-geolocation-example-app/863470):
>
> Needing a new backend solution for your [#madeWithThunkable](https://twitter.com/hashtag/madeWithThunkable?src=hashtag_click) app? Try [@backendless](https://twitter.com/Backendless) today! Get rid of the limitations imposed by [@airtable](https://twitter.com/airtable) and move beyond the difficulty of [@firebase](https://twitter.com/Firebase) (though each has their place)! Check out this template CRUD application [https://x.thunkable.com/copy/83612c4b430ae494eadee2edf27f3b1b[Depricated]](https://x.thunkable.com/copy/83612c4b430ae494eadee2edf27f3b1b%5BDepricated%5D) This will get you up and running quick! You will only need to change out the [@backendless](https://twitter.com/Backendless) URL’s. The rest is up to you in terms of styling or incorporating it further! I wi…

---

<div class="post-metadata">

**Author:** ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)\
**Post date:** [October 2, 2020, 10:47am UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/5 "2020-10-02T10:47:16Z")

</div>

Here is a general page on the calculations. Then you can use Thunkable’s math functions.

> **[Program for distance between two points on earth - GeeksforGeeks](https://www.geeksforgeeks.org/program-distance-two-points-earth/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

---

<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 2, 2020, 10:53am UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/6 "2020-10-02T10:53:07Z")

</div>

That is a good suggestion @drted, did you check out the app I linked to above? it demonstrates how to tie into a backend that does the work for you. It does search in radius and loads markers within a set radius of the user. all the phone does is send the query and load the markers!

Haversine is good. but iit only tells you distance. you’d have to do this for each point on the map, right?

```auto
function haversineDistance(coords1, coords2, isMiles) {
  function toRad(x) {
    return x * Math.PI / 180;
  }

  var lon1 = coords1[0];
  var lat1 = coords1[1];

  var lon2 = coords2[0];
  var lat2 = coords2[1];

  var R = 6371; // km

  var x1 = lat2 - lat1;
  var dLat = toRad(x1);
  var x2 = lon2 - lon1;
  var dLon = toRad(x2)
  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;

  if(isMiles) d /= 1.60934;

  return d;
}

```

---

<div class="post-metadata">

**Author:** ![drted](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/drted/32/92694_2.png) [@drted](https://community.thunkable.com/u/drted)\
**Post date:** [October 2, 2020, 10:57am UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/7 "2020-10-02T10:57:11Z")

</div>

[jared](http://community.thunkable.com/u/jared), yes the backendless WebAPI is super slick and MUCH easier than “Rolling your own” solution. Sometimes it is nice (and FUN!) to do it yourself. Doing it within thunkable does have the advantage of reducing external dependencies. Thunkable has enough of those that we have all felt the pain of a third party modification changing thunkable behavior breaking our apps. 😉

---

<div class="post-metadata">

**Author:** ![bobsunscreen](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/bobsunscreen/32/85667_2.png) [@bobsunscreen](https://community.thunkable.com/u/bobsunscreen)\
**Post date:** [October 2, 2020, 2:31pm UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/8 "2020-10-02T14:31:30Z")

</div>

Thanks @jared and @drted! I’ll play around with these methods.

---

<div class="post-metadata">

**Author:** ![paulovaz72](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/paulovaz72/32/119071_2.png) [@paulovaz72](https://community.thunkable.com/u/paulovaz72)\
**Post date:** [June 17, 2022, 4:06am UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/9 "2022-06-17T04:06:26Z")

</div>

In gratitude to your tip and commitment with the community, following the function ported in to Thunkable blocks:

 ![Captura de tela de 2022-06-17 01-04-36](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/d/8d691ec12bcd29537b01c50ba8fb98b55ea1fde5.png)

Regards,

Paulo Vaz

---

<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:** [July 28, 2022, 2:22pm UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/10 "2022-07-28T14:22:44Z")

</div>

I wanted to share a fun solultion that I created for another project

[https://us-central1-scorecard-x.cloudfunctions.net/returnDistance?long1=45&lat1=-83&long2=22&lat2=-40&miles=true](https://us-central1-scorecard-x.cloudfunctions.net/returnDistance?long1=45&lat1=-83&long2=22&lat2=-40&miles=true)

You can feed it the lat and long for both location 1 and 2, true/false for returning miles or KM. this will return an object that has the distance!

Quick and easy. each response takes apx 44 seconds when the function is warm. If you are doing a 1 off calculation, it takes about a second.

i also replicated it in cloudflare for fun. always faster than the one above

- [distancefinder.codelessconsultant.workers.dev?long1=45&lat1=-83&long2=22&lat2=-40&miles=true](https://distancefinder.codelessconsultant.workers.dev?long1=45&lat1=-83&long2=22&lat2=-40&miles=true)

---

<div class="post-metadata">

**Author:** ![godtap](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/godtap/32/181507_2.png) [@godtap](https://community.thunkable.com/u/godtap)\
**Post date:** [June 8, 2023, 5:38pm UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/11 "2023-06-08T17:38:42Z")

</div>

Jared, it’s been a while. Working under a new account name right now instead of Hhuman. I wonder if you can reshare this app project that ties into a backend since that link has expired already. Thanks beforehand. PEace

---

<div class="post-metadata">

**Author:** ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)\
**Post date:** [January 15, 2024, 9:16pm UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/12 "2024-01-15T21:16:07Z")

</div>

Hey Hey Jared,

Thanks for sending the page. But yes it looks expired.

---

<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:** [January 16, 2024, 12:50am UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/13 "2024-01-16T00:50:02Z")

</div>

Yooooo @cresto.pgaxq2

What do you mean?

> [@jared](#):
>
> distancefinder.codelessconsultant.workers.dev?long1=45&lat1=-83&long2=22&lat2=-40&miles=true

This is working

---

<div class="post-metadata">

**Author:** ![cresto.pgaxq2](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cresto.pgaxq2](https://community.thunkable.com/u/cresto.pgaxq2)\
**Post date:** [January 16, 2024, 1:15am UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/14 "2024-01-16T01:15:56Z")

</div>

Thanks

Is it the response from the webpage (3013.8516790252716)?

Thanks

Using more backendless, “which used before”

I see code in there from Paulo

---

<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:** [January 16, 2024, 1:21am UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/15 "2024-01-16T01:21:02Z")

</div>

yessir.

you’d need to swap out the lat longs in the url and decide on km or miles with the true/false flag at the end

---

<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, 11:27am UTC](https://community.thunkable.com/t/solved-how-do-i-find-add-map-markers-nearest-to-the-user-haversine-formula/598411/16 "2024-11-08T11:27:42Z")

</div>


