javascript – 解析云查询以获取具有最接近GeoPoint的对象

我在编写Parse查询以获取具有对输入的GeoPoint的CLOSEST的GeoPoint的Parse对象时遇到问题.目前,代码似乎返回最近创建的对象.

码:

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

我想要的是first()查询返回具有中心点键中的CLOSEST GeoPoint的对象.

我尝试添加query.near(“centerPoint”,geoPoint)和query.limit(1)也无济于事.

我见过iOS PFQueries调用whereKey:nearGeoPoint:withinMiles:可以根据最近的GeoPoints返回排序.是否有像这样工作的JavaScript等价物?

最佳答案
你会试试吗?如果所有距离都相同,则Parse不会按您所需的精度排序.

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

这导致十个最近距离.

聊天之后,似乎距离没有被分类的原因是Parse只能以几厘米的精度进行排序.用户看到的差异小于此.

转载注明原文:javascript – 解析云查询以获取具有最接近GeoPoint的对象 - 代码日志