I am trying to find all users within a certain distance of the current user's PFGeopoint. The query returns objects when using:
query!.whereKey("addressPoint", nearGeoPoint: userGeopoint)
but it returns no objects when using:
query!.whereKey("addressPoint", nearGeoPoint: userGeopoint, withinMiles: 100)
There are almost 20 users within 100 miles of the simulator's position in Cupertino, so that isn't the issue.
Here is the full code:
override 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 { print(error) } }) }
to get user location it can take couple seconds, that is why PFGeoPoint.geoPointForCurrentLocationInBackground is not running on the main thread, put your query inside that and it will work... Also i think that the query by distance is limited only to 100 Miles
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.