title: How to use my location to calculate distance between two points using latitude and longitude
body: <p>I'm trying to get my REST API to only send me back items that are within a certain radius. I've written code to calculate the distance between two points and only send me the ones within the desired radius. However, as of now I'm using an arbitrary latitude and longitude as the point of comparison. How can I implement using my own location into this algorithm?</p>
  
  <pre><code>app.get('/posts', function(req, res) { 
  
  // Get 'posts' collection from db
  db.collection('posts', function(err, collection) { 
  
      // Get all documents in the 'posts' collection
      collection.find().toArray(function(err, items) {
  
          // Documents to send back
          var results = []
  
  //Function that calculates distance between two points.
      function calculateDistance (lat1, lat2, lon1, lon2) {
          var dLat = deg2rad(lat2-lat1);  
          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 = (6371 * c); // Distance in km
          var e = (d/1.6) //Convert distance in km to miles.
              return e 
      }
  
          // Iterate over each document and check if the distance is correct
          for (var i = 0; i &lt; items.length; i++) {
              var item = items[i]
               if ((calculateDistance(item.latitude, 43.7035798, item.longitude, -72.2887838) &gt; 25)) {
                   results.push(item)
               }
  
          }          
          res.send(results);
      });
  });
  });
  </code></pre>