如何使用 PFGeoPoint 在给定的距离半径内获取用户
How can I get users within a given radius of distance using PFGeoPoint
我在 Parse 中有 PFGeoPoint 并且有一个 serachView,用户将在其中输入以英里为单位的半径。因此它将 return 一个在 dat 半径范围内的用户列表。
我知道我们可以从坐标获取距离,但我不知道如何让所有用户都到那里并一一检查。
如果有人能给我一些建议,那将对我有很大帮助,因为我是 iOS 和 Parse 的新手。
已编辑
Parse 实际上有一个简单的解决方案:
int radiusInKilometers = 400; //Example
PFGeoPoint * myGeoPoint = [PFUser currentUser][@"geoPoint"]; // Your geoPoint
PFQuery *query = [PFUser query];
[query whereKey:@"geoPoint" nearGeoPoint:myGeoPoint withinKilometers:radiusInKilometers];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"Successfully retrieved %d scores.", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
// DISTANCE
PFGeoPoint * geoPoint1 = [PFUser currentUser][@"geoPoint"];
PFGeoPoint * geoPoint2 = userObject[@"geoPoint"];
CLLocation *location1 = [[CLLocation alloc]
initWithLatitude:geoPoint1.latitude
longitude:geoPoint1.longitude];
CLLocation *location2 = [[CLLocation alloc]
initWithLatitude:geoPoint2.latitude
longitude:geoPoint2.longitude];
CLLocationDistance distance = [location1 distanceFromLocation:location2];
// Print out the distance
NSLog(@"There's %d km between you and this user", distance);
}
} else {
// Details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
您可以将 withinKilometers
更改为 withinMiles
。
我在 Parse 中有 PFGeoPoint 并且有一个 serachView,用户将在其中输入以英里为单位的半径。因此它将 return 一个在 dat 半径范围内的用户列表。
我知道我们可以从坐标获取距离,但我不知道如何让所有用户都到那里并一一检查。
如果有人能给我一些建议,那将对我有很大帮助,因为我是 iOS 和 Parse 的新手。
已编辑
Parse 实际上有一个简单的解决方案:
int radiusInKilometers = 400; //Example
PFGeoPoint * myGeoPoint = [PFUser currentUser][@"geoPoint"]; // Your geoPoint
PFQuery *query = [PFUser query];
[query whereKey:@"geoPoint" nearGeoPoint:myGeoPoint withinKilometers:radiusInKilometers];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"Successfully retrieved %d scores.", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
// DISTANCE
PFGeoPoint * geoPoint1 = [PFUser currentUser][@"geoPoint"];
PFGeoPoint * geoPoint2 = userObject[@"geoPoint"];
CLLocation *location1 = [[CLLocation alloc]
initWithLatitude:geoPoint1.latitude
longitude:geoPoint1.longitude];
CLLocation *location2 = [[CLLocation alloc]
initWithLatitude:geoPoint2.latitude
longitude:geoPoint2.longitude];
CLLocationDistance distance = [location1 distanceFromLocation:location2];
// Print out the distance
NSLog(@"There's %d km between you and this user", distance);
}
} else {
// Details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
您可以将 withinKilometers
更改为 withinMiles
。