Search code examples
node.jsdictionarygpsgeolocation

Determine which users are close to one user, using geolocation


I am wondering what is the best way to determine and calculate, which users are close to one user, using geolocation and their GPS coordinates.

Given the main user GPS coordinates, I know how to calculate a "bounding box" around his position. I can store that bounding box coordinates in a database, for each user profile. But then, how can I determine which users are close to the user sending the request ?

Is there any formula that would allow me to calculate a "box" around a GPS location, given a distance, so every othe GPS position located within THAT box and applying this formula, would render the same box ?

Doing this would allow me to simply ask my database which users are pretending to be in this box.


Solution

  • I finally decided to use the $geoNear aggragation provided by MongoDB. This works perfectly, as I can set up a minimum and maximum distance.

    Note that using Mongoose in Node.js, this functionnality is available through Schema.geoNear(...).