Sort users by distance from current user reactively in Meteor?

I have already been facing issues in meteor when dealing with reactive sorting of a publication according to various data. Every time, I never found a real answer to what I was trying to achieve and I ended up giving up and just deciding "not to build my app that way", which always makes me really sad.

Now maybe I can phrase this problem simply enough that it could get a constructive and very interesting answer. I think it sounds like it should be possible to be done in a Meteor app, so here it is:

I store the locations of my users in "lat" and "lon" variables in my Accounts.users collection. I want the current user to get a list of, say, the 10 closest users to him or her. How would I do this, considering the seemingly complicated set of operations needed to get that distance from latitude and longitude?

As far as I know, it is pretty much impossible to insert such an equation in a MongoDB query condition. So how do other people achieve this feat?

I would appreciate any insight on this problem, as theoretical as it may be.

Answers:

Answer

Look at my working example localgeo and sources on github

You can find like this:

   var nearMarkers = allMarkers.find({
      location: {
        $near: {
          $geometry: {
            type: "Point",
            coordinates: location
          },
          $maxDistance: radius
        }
      }
    });

Location is this type of MongoDB. And remember: in MongoDB Lng - first, Lat - second, I don't know why. I do it on client side, but you can make publication in the same way, depend on radius.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.