ElYeante
ElYeante

Reputation: 1755

Query between distances in Parse

I would like to search places between two given distances, using GeoPoints. Using the currently api, I think I could make something like this pseudo algorithm:

query.whereWithinKilometers("directions", GlobalData.ownerGeoPoint, MAXIMUM distance);

MINUS

query.whereWithinKilometers("directions", GlobalData.ownerGeoPoint, MINIMUM distance);

How can I translate to real code?

Upvotes: 0

Views: 873

Answers (2)

ElYeante
ElYeante

Reputation: 1755

Finally I found a way to do it with a parse query.

// Do not want locations further than maxDistance
ParseQuery query = ParseQuery.getQuery("MyData");
query.whereWithinKilometers("location", userGeoPoint, maxDistance);

// Do not want locations closer than minDistance
ParseQuery<ParseObject> innerQuery = ParseQuery.getQuery("MyData");
innerQuery.whereWithinKilometers("location", userGeoPoint, minDistance);
query.whereDoesNotMatchKeyInQuery("objectId", "objectId", innerQuery);

Upvotes: 2

st.derrick
st.derrick

Reputation: 4919

Likely you'll have to pull in everything within the maximum distance from Parse to your app, then inside your app filter out anything that is closer than the minimum.

Thankfully Parse includes some distance measuring as part of PFGeoPoint. Check out the three methods here.

  1. distanceInRadiansTo:
  2. distanceInMilesTo:
  3. distanceInKilometersTo:

So your pseudocode is:

(After you get back everything within the outer radius)

Make a new array (var) called finalList
For i = 0; i < objects.count; i++ {
   var gp: PFGeoPoint = objects[i]
   if gp.distanceInMilesTo:originalGeoPoint >= minimumRadiusInMiles {
      finalList.push(gp)
   }
}

Upvotes: 1

Related Questions