4

I am having issues writing a Parse query to get a Parse object with a GeoPoint that is CLOSEST to the inputted GeoPoint. Currently, the code appears to be returning the most recently created object.

Code:

// check Parse for infections around passed GeoPoint
Parse.Cloud.define("InfectionCheck_BETA", function(request, response) {

var returnCount;

var geoPoint = request.params.geoPoint;
var query = new Parse.Query("InfectedArea");
query.withinMiles("centerPoint", geoPoint, 1); // check for infections within one mile

Parse.Promise.as().then(function() {
    // query for count of infection in area, this is how we get severity
    return query.count().then(null, function(error) {
        console.log('Error getting InfectedArea. Error: ' + error);
        return Parse.Promise.error(error);
    });

}).then(function(count) {
    if (count <= 0) {
        // no infected areas, return 0
        response.success(0);
    }
    returnCount = count;
    return query.first().then(null, function(error) {
        console.log('Error getting InfectedArea. Error: ' + error);
        return Parse.Promise.error(error);
    });

}).then(function(result) {
    // we have the InfectedArea in question, return an array with both
    response.success([returnCount, result]);

}, function(error) {
    response.error(error);
});
});

What I want is for the first() query to return the object with the CLOSEST GeoPoint in the centerPoint key.

I have tried adding query.near("centerPoint", geoPoint) and query.limit(1) to no avail as well.

I have seen iOS PFQueries calling whereKey:nearGeoPoint:withinMiles: that supposably returns sorted based on nearest GeoPoints. Is there a JavaScript equivalent that works like this?

Andrew Robinson
  • 3,404
  • 3
  • 20
  • 26

1 Answers1

6

Would you try this? If all the distances are the same, then Parse isn't sorting to the precision that you need.

// check Parse for infections around passed GeoPoint
Parse.Cloud.define("InfectionCheck_BETA", function(request, response) {
    var geoPoint = request.params.geoPoint;
    var query = new Parse.Query("InfectedArea");
    query.near("centerPoint", geoPoint);
    query.limit(10);
    query.find({
        success: function(results) {
            var distances = [];
            for (var i = 0; i < results.length; ++i){
                distances.push(results[i].kilometersTo(geoPoint));
            }
            response.success(distances);
        }, 
        error: function(error) {
            response.error("Error");
        }
    });
});

This results in the ten closest distances.

After chatting, it seems that the reason that the distances weren't being sorted is that Parse only sorts with accuracy of a few centimeters. The differences the user was looking at were less than that.

Dehli
  • 5,950
  • 5
  • 29
  • 44
  • I tried that before, while keeping the `query.withinMiles` as well which didn't work. I do need to specify the within one mile, however. If this works, I guess I could do a count query on `query.withinMiles` and then a separate query with the `query.near` if the count was > 0... – Andrew Robinson Sep 16 '14 at 22:08
  • Want to talk about this [here](http://chat.stackoverflow.com/rooms/61287/parse-com)? – Dehli Sep 16 '14 at 22:12
  • Still didn't work with the above code. Went to link to discuss. – Andrew Robinson Sep 16 '14 at 22:18
  • I thought the "distance is too close" issue was going to be it, sadly it wasn't. Above returned: 0.000024425918286011603, 0, 0.000016283945178727897, 0.000008141973107283708, 3.349423362835491. Obviously the 0 should come first, then others before the 0.00002... – Andrew Robinson Sep 17 '14 at 13:08
  • Even though this function returns differences in distance, the Parse function does not get down to the location granularity I was asking for which is why the code wasn't working. Marking correct since it pointed me in the right direction. – Andrew Robinson Sep 17 '14 at 15:51
  • hey guys, appreciate your help I am trying to send a query for the function mentioned above but I get no results. headers = {} params.headers = headers local message1 = {} message1= { ["__type"]= "GeoPoint", ["latitude"]= -42.785311, ["longitude"]= 147.527295 } params.body = json.encode ( message1 ) – Amir May 27 '15 at 09:18
  • is "centerPoint" the column or class name in parse or simply tells closest distance? Do I need to change this value? – Amir May 27 '15 at 09:20
  • @Amir `centerPoint` is the name of the class. Could you create a new question with more details please? – Dehli May 27 '15 at 15:38
  • Thanks Dehli. I could work it out. I had to replace centerPoint with my column name and get rid of that kilometer method which returned error. Otherwise it is working fine now. – Amir May 28 '15 at 06:06