Using Parse Javascript SDK Query GeoPoint inMiles

I am building a Parse application and want all objects to be within a certain distance from one object using GeoPoints. Seems simple, but the following code returns [] (No matches):

app.get('/photos', function(req, res) {

   var Photo = Parse.Object.extend("Photo");

   var query = new Parse.Query(Photo);
   query.equalTo("owner", Parse.User.current());
   query.find({
     success: function(results) {
       // Do something with the returned Parse.Object values
       //res.send(results);
       var photo = results[0];

       // Create a query for places
      var Photo = Parse.Object.extend("Photo");
      var query = new Parse.Query(Photo);
      // Interested in photos taken near this photo.
      query.withinMiles("coordinates", photo.coordinates, 500000);

      // Final list of objects
      query.find({
        success: function(photoObjects) {
           res.send(photoObjects);
        },
        error: function(error) {
         alert("Error: " + error.code + " " + error.message);
        }
      });
     },
     error: function(error) {
       alert("Error: " + error.code + " " + error.message);
     }
   });
});

      

Oddly enough, if I change the line query.withinMiles("coordinates", photo.coordinates, 500000);

to query.near("coordinates", photo.coordinates);

then it works flawlessly and returns all photos (they each have a GeoPoint within 5 miles of everyone else in the sample dataset, so I used a maximum distance of 500,000 to test the extent of the problem).

The problem is that I need to constrain a certain radius and this is what I thought was "inMiles". Any ideas what I might be doing wrong?

+3


source to share


1 answer


If anyone finds this and shares my concerns, the following problem has occurred:

I assumed that all the "columns" of data objects in the parsing are treated as attributes. those. you can access them using. operator (photo, coordinates).



In fact, you have to access them via photo.get ("coordinates") to get the actual objects contained in the data structure.

Not sure why the above scenario worked for the near function, but the behavior still works correctly when I started using the get accessor method on Parse data objects.

+4


source







All Articles