我在编写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()查询使用centerPoint
键中的CLOSEST GeoPoint返回对象。
我尝试添加query.near("centerPoint", geoPoint)
和query.limit(1)
也无济于事。
我已经看到iOS PFQueries调用whereKey:nearGeoPoint:withinMiles:
,它可以根据最近的GeoPoints返回排序。是否有像这样的JavaScript等价物?
答案 0 :(得分:6)
// 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只能精确地分类几厘米。用户看到的差异小于此。