0

I'm building a simple API that stores locations with coordinates, and I want to query the locations based on their distance to the user's position.

Right now, I get users position with:

var geolocation = angular.module('geolocation', []);        
navigator.geolocation.watchPosition(render);
var lat, lon = 0;
function render(pos) {
    lat = pos.coords.latitude;
    lon = pos.coords.longitude;
    console.log(lat +","+ lon);
}

And i try to query the locations with:

exports.find_all_locations_near = function(req, res) { 
const $minDistance = req.query.minDistance;
  const $maxDistance = req.query.maxDistance;
  if ($minDistance > 0) 
  {
    Location.find({
      geolocation: 
      { 
          $near: 
          {
              $geometry: 
              {
                  type: "Point",
                  coordinates: [lat, lon]
              },
              $minDistance,
              $maxDistance
          }
      }
    }), function(err, location) {
      if (err) {
        //console.log(err);
        res.send(err);
      }
      else if (location =='')
        //res.json({ message: 'No location was found.' });
        res.json({ message: 'No location was found.' });
      else
      {
        res.json(location);
        res.json({ message: 'The end.' });
      }          
    };
  } else 
  {
    Location.find({}, function(err, location) {
      if (err)
        res.send(err);
      res.json(location);
      res.json(req.params.minDistance);
    });
  }  
}

I'm displaying the results like this:

<tr ng-repeat="search in searchLocations | orderBy:sortType:sortReverse">
              <td>{{ search.locationName }}</td>
              <td>{{ search.distance}}</td>               
</tr>

How do I query the locations with the calculated distance? And how can I make the query work properly, since I'm getting connection refused?

The github repo in case you need more info https://github.com/supermonteiro/geolocationAPI

SuperMonteiro
  • 55
  • 1
  • 10
  • See [`$geoNear`](https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/) which is an aggregation operator. Only `.aggregate()` alters the structure of returned documents ( i.e adds a field called distance ) . The `.find()` method is not capable of making such alterations. – Neil Lunn Aug 19 '17 at 01:55
  • This doesnt not return the distance between the user and the locations. It only helps me find those under max distance. – SuperMonteiro Aug 19 '17 at 02:33
  • Yes it does return the distance. There is actually a **mandatory** parameter called `distanceField`, so there actually is no other option other than to have a field included in the results that represents the distance from the queried point against any locations found "near". – Neil Lunn Aug 19 '17 at 02:40
  • I tried to follow the example but i`m getting connection refused. here is what I tried: Location.aggregate([ { $geoNear: { near: { type: "Point", coordinates: [ lat , lon ] }, distanceField: "dist.calculated", maxDistance: $maxDistance, minDistance: $minDistance, query: { type: "public" }, includeLocs: "dist.location", spherical: true } } ]) Do i need to change something on my routes? I cant figure out the issue. – SuperMonteiro Aug 19 '17 at 03:30

0 Answers0