我在Parse中有PFGeoPoint,并且有一个serachView,用户将以英里为单位输入半径。因此,它将返回一个在距离的数据半径范围内的用户列表。
我知道我们可以从坐标中获取距离,但我不知道如何让所有用户在那里逐一检查。
如果有人可以向我推荐一些内容,那么对我来说这将是一个很大的帮助,因为我是iOS和Parse的新手。
答案 0 :(得分:2)
<强> EDITED 强>
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
。