Analyze a cloud query to get an object with the nearest GeoPoint

I'm having trouble writing a Parse request to get a Parse object with a GeoPoint, which is CLOSEST for an injected GeoPoint. Currently the code seems to be returning the last object created.

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);
});
});

      

I want the first () request to return an object with a CLOSEST GeoPoint in centerPoint

.

I tried adding query.near("centerPoint", geoPoint)

and query.limit(1)

to unsuccessfully.

I've seen an iOS PFQueries caller whereKey:nearGeoPoint:withinMiles:

that supposedly returns sorted based on the nearest GeoPoints. Is there a JavaScript equivalent that works like this?

+3


source to share


1 answer


Will you try? If all distances are the same then Parse will not sort the precision you want.

// 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 the conversation, it seems that the reason the distances are not sorted is because Parse only sorts to within a few centimeters. The differences the user was looking at was less than that.

+4


source







All Articles