I tried to add a $geoWithin
constraint to a Parse.Query
like so:
var query = new Parse.Query("MyCollection");
var jsonQuery = query.toJSON();
jsonQuery.where.location = {
"$geoWithin": {
"$centerSphere": [
[geoPoint.longitude, geoPoint.latitude],
10 / 6371.0
]
}
};
query.withJSON(jsonQuery);
It throws the error:
bad $geoWithin value; $polygon should contain at least 3 GeoPoints.
The reason is apparently that Parse Server's Mongo Transformer only accepts a polygon parameter with $geoWithin
.
How can I add a query constraint that limits the results to documents with a location
field within a certain radius in km of a geo coordinate?
It turned out that Parse Server did not support this type of query. But since Parse Server
is an open source project on Github, I added the feature*.
The code in the question works and the same can be achieved setting the new sorted
parameter in geo queries to false
:
var location = new Parse.GeoPoint(37.708813, -122.526398);
var distance = 5;
var sorted = false;
var query = new Parse.Query(MyCollection);
query.withinKilometers("location", location, distance, sorted);
*Feature not yet available as of Parse Server v2.8.2 and JS SDK v1.11.1, waiting for merge of pull requests.