title: Mongo $geoWithin Radians
body: <p>Struggling to calculate radians of a circle use mongoose/mongodb geoWithin query.  I have the user's location, and I the result furthest away from that user.  From those two points I need to calculate radians so I can search in a full circle around that user reaching out to furthest result.  Hopefully that makes sense. :)</p>
  
  <pre><code>    //get closest item to user (just because)
      var closestLon = vendorPosts[0].location.long;
      var closestLat = vendorPosts[0].location.lat;
  
      //get furthest item from user
      var furthestLon = vendorPosts[vendorPosts.length - 1].location.long;
      var furthestLat = vendorPosts[vendorPosts.length - 1].location.lat;
  
      //User is located here
      var userLat = req.query.lat;
      var userLon = req.query.long;
  
      //Number of kilometers furthest item is from user
      var maxDistanceKm = getDistanceFromLatLonInKm(userLat, userLon, furthestLat, furthestLon);
  
      //Attempting to convert to rads but results never appear, is this correct calculation?
      var maxDistanceRadians = maxDistanceKm/6371;
  
      Model.find({
          $text: { $search: searchterm, $language: 'english' },
          coordinates :
               { $geoWithin :
                   {
                       $center : [ [ userLon, userLat ] , maxDistanceRadians ]
                   }
               }
          } , { score: {
                  $meta: "textScore"
              }
          }, function (err, results) {
              ....
          });
  </code></pre>
  
  <p>Using these famous haversine formulas/utils</p>
  
  <pre><code>    function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
          var R = 6371; // Radius of the earth in miles
          var dLat = deg2rad(lat2-lat1);  // deg2rad below
          var dLon = deg2rad(lon2-lon1);
          var a =
                Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(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; // Distance in km
          return d;
      }
  
      function deg2rad(deg) {
          return deg * (Math.PI/180)
      }
  </code></pre>