在Parse中查询距离

时间:2015-02-26 21:11:23

标签: android parse-platform pfquery geopoints

我想使用GeoPoints搜索两个给定距离之间的位置。使用当前的api,我想我可以制作类似这种伪算法的东西:

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

MINUS

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

如何翻译成真实代码?

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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

值得庆幸的是,Parse包括一些距离测量,作为PFGeoPoint的一部分。查看三种方法here

  1. distanceInRadiansTo:
  2. distanceInMilesTo:
  3. distanceInKilometersTo:
  4. 所以你的伪代码是:

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