withinMiles不使用Swift中的Parse PFGeopoint查询

时间:2016-07-29 01:57:12

标签: ios swift xcode parse-platform coordinates

我试图在当前用户的PFGeopoint的某个距离内找到所有用户。使用时查询返回对象:

query!.whereKey(“addressPoint”,nearGeoPoint:userGeopoint)

但使用时不返回任何对象:

query!.whereKey(“addressPoint”,nearGeoPoint:userGeopoint,withinMiles:100)

模拟器在库比蒂诺的位置100英里内有近20名用户,所以这不是问题。

以下是完整代码:

覆盖func viewDidLoad(){

    super.viewDidLoad()

var userGeopoint = PFGeoPoint()

    PFGeoPoint.geoPointForCurrentLocationInBackground {
        (geoPoint: PFGeoPoint?, error: NSError?) -> Void in

        if error == nil {
            userGeopoint = geoPoint!
            //print(userGeopoint)
        }
    }

    let query = PFUser.query()
    query!.whereKey("addressPoint", nearGeoPoint: userGeopoint, withinMiles: 1000)
    query!.limit = 20
    query!.orderByDescending("updatedAt")
    query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

        if error == nil {

        if let users = objects {
        for object in users {
                if let user = object as? PFUser {
                    if user != PFUser.currentUser() {
                        print(user["addressPoint"])
                        }
                    }
            }

}             } else {                 打印(错误)             }         }) }

1 个答案:

答案 0 :(得分:0)

获取用户位置可能需要几秒钟,这就是为什么 PFGeoPoint.geoPointForCurrentLocationInBackground 没有在主线程上运行,将您的查询放在其中并且它将起作用...而且我认为距离的查询仅限 <100英里

PFGeoPoint.geoPointForCurrentLocationInBackground {
    (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
    if error == nil {
        let query = PFUser.query()
        query!.whereKey("addressPoint", nearGeoPoint: geoPoint, withinMiles: 100)
        //... rest of the query
    }
}