在 Parse 中查询距离

Query between distances in Parse

我想使用 GeoPoints 搜索两个给定距离之间的地点。使用目前的api,我想我可以做出类似这个伪算法的东西:

query.whereWithinKilometers("directions", GlobalData.ownerGeoPoint, 最大距离);

减去

query.whereWithinKilometers("directions", GlobalData.ownerGeoPoint, 最小距离);

我怎样才能翻译成真正的代码?

您可能必须将 Parse 到您的应用程序的最大距离内的所有内容都拉入,然后在您的应用程序内部过滤掉任何比最小距离更近的内容。

谢天谢地,Parse 包含一些距离测量作为 PFGeoPoint 的一部分。查看三种方法 here.

  1. 距离InRadiansTo:
  2. distanceInMilesTo:
  3. distanceInKilometersTo:

所以你的伪代码是:

(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)
   }
}

最后我找到了一种使用解析查询的方法。

// 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);