Question

I'm putting together a mobile meteor application that I want to use to list local (say within a 20 mile radius) amenities. I have a collection of these amenities with corresponding latlng data - I'd like to be able to pass the app my current location (using Cordova) and generate a list (/collection?) that is sorted closest first.

I have two specific problems that I'd really appreciate some advice on!

Can I use mongo's $near for this or should I be using a node.js add-on (eg 'GeoLib' - https://github.com/manuelbieh/geolib) to do the distance calculation?

How do I generate a temporary (locally stored) collection of these locations to display in my list? Presumably if I don't use $near I have to iterate through my locations, calculating the distance on all of them and then returning any where the distance is under a certain threshold, but this seems like an expensive way of doing it when my location list will grow and grow.

Sorry, this is the first time I've attempted something like this; I'd really appreciate any advice from a more seasoned developer!

EDIT - MY CODE (WHY IS IT NOT WORKING?!)

I'm storing locations like this in a collection:

Beaches.insert({ name: 'Venice Beach CA', geometry: { type: "Point", coordinates: [-118.473314,118.473314] } });

...

Beaches._ensureIndex({'geometry.coordinates':'2d'}, function(err, result) { if(err) return console.dir(err); });

I'm querying these entries like this (passing in the lat and lng):

getNearBeaches = function(lng,lat) { return Beaches.find({'geometry.coordinates': { $near: { $geometry: { type: "Point", coordinates: [lng,lat] } }, $maxDistance: 20000 //meters } }) };

I can list my collection with a straight find() but my location search returns nothing, even if I set the $maxDistance to a huge number, or search directly on an already stored set of coords.

What have I done wrong??

Was it helpful?

Solution

You can fetch the current location, send it to server and subscribe to collection, filtered by mongodb $near:

Meteor.subscribe("amenities", {latlng: Session.get("latlng")});

And smth like this:

Meteor.publish("amenities", function (latlng) {
   return Amenities.find({
      location: {
        $near: {
          $geometry: {
            type: "Point",
            coordinates: latlng
          },
          $maxDistance: 20000   //meters
        }
      }
    });
});

You must have correct data type and indexes. And field location (-:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top