我试图找到当前用户的 PFGeopoint 一定距离内的所有用户。使用时查询返回对象:
query!.whereKey("addressPoint", nearGeoPoint: userGeopoint)
但它在使用时不返回任何对象:
query!.whereKey("addressPoint", nearGeoPoint: userGeopoint, withinMiles: 100)
库比蒂诺模拟器位置 100 英里范围内有近 20 个用户,所以这不是问题。
完整代码如下:
覆盖函数 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"])
}
}
}
} } 别的 { 打印(错误) } })
最佳答案
获取用户位置可能需要几秒钟,这就是为什么 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
}
}
关于ios - withinMiles 无法在 Swift 中使用 Parse PFGeopoint 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38649351/