tfcstack
tfcstack

Reputation: 138

Parse.com Anywall tutorial order posts according to the distance to the active user

I am a begginer in parse, I have been following parse Anywall tutorial for android https://www.parse.com/tutorials/anywall-android

In the example code, there is a customized query, which order items according to the order in which they were created.

// Set up a customized query
ParseQueryAdapter.QueryFactory<AnywallPost> factory =
    new ParseQueryAdapter.QueryFactory<AnywallPost>() {
      public ParseQuery<AnywallPost> create() {
        Location myLoc = (currentLocation == null) ? lastLocation : currentLocation;
        ParseQuery<AnywallPost> query = AnywallPost.getQuery();
        query.include("user");
        query.orderByDescending("createdAt");
        query.whereWithinKilometers("location", geoPointFromLocation(myLoc), radius
            * METERS_PER_FEET / METERS_PER_KILOMETER);
        query.setLimit(MAX_POST_SEARCH_RESULTS);
        return query;
      }
    };

In this partion of the code, I already have the location of the user (myLoc var), and each object retrieved bring its location (under "location" field). My question is, how can I use this fields to order posts according to their distance to the active user?

Upvotes: 1

Views: 878

Answers (2)

tfcstack
tfcstack

Reputation: 138

Amswering myself :)

https://parse.com/docs/android_guide#geo-query

Now that you have a bunch of objects with spatial coordinates, it would be nice to find out which objects are closest to a point. This can be done by adding another restriction to ParseQuery using whereNear.

ParseQuery<ParseObject> query = ParseQuery.getQuery("PlaceObject");
query.whereNear("location", userLocation);

Upvotes: 0

Timothy Walters
Timothy Walters

Reputation: 16884

Distance queries will default to sorting by nearest to farthest, unless you specify some other sort order using orderByAscending()/orderByDescending().

Simply remove any orderBy statements from your query and you'll get the sorting you want.

Upvotes: 2

Related Questions