Fred Vanelli Fred Vanelli - 1 year ago 120
Android Question

Parse query.whereNear and query.whereWithinKilometers both return empty object list

First, I'm using Parse Server on localhost and my client is Parse Android SDK.

I have a class "Place" with GeoPoint field called "location". This class has 50 rows, all of them with "location" like:
-22.xxxxxx | -42.xxxxxx

When I try to query all rows, without

filter, I get all the 50. But when I add the filter
, I get a empty

ParseQuery<ParseObject> query = ParseQuery.getQuery("Place");
Log.d("LOG 1", String.valueOf(gp.getLatitude()));
Log.d("LOG 2", String.valueOf(gp.getLongitude()));
query.whereWithinKilometers("location", gp, 150);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
Log.d("LOG 3", objects.toString());
if (e == null) {
if (objects.size() > 0) {
for (int i = 0; i < objects.size(); i++) {
//do something here...
} else {
Log.d("Query error", e.getMessage());

The logs "LOG 1" and "LOG 2" shows my correct coordinates, so the
Geopoint is correct.

The "LOG 3" isn't shown because "objects.toString()" throws NullPointerException.


That exact same code, without the
works perfectly. And de callback takes less than 1 second.

, the callback takes 5 to 10 seconds, and then returns empty object. I've tried 15, 150, 1500, 15000 kilometers and nothing is returned. And all places in that class are in my city, so, 10 kilometers would do the trick.

I can't think of anything else to check.

Answer Source

I've found the solution. The geopoint queries need a specific "2dsphere" index on MongoDB to work.

The solution is to execute the following code on MongoDB script shell:

    db.Place.ensureIndex({ location: "2dsphere" });

Where "Place" is the class/collection and "location" is the geopoint field.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download