1

我在 Parse 中有 PFGeoPoint,并且有一个 serachView,用户将在其中输入以英里为单位的半径。因此,它将返回距离半径范围内的用户列表。

我知道我们可以从坐标中获取距离,但我不知道如何让所有用户到那里并一一检查。

如果有人能给我一些建议,那么这对我来说将是一个很大的帮助,因为我是 iOS 和 Parse 的新手。

4

1 回答 1

2

已编辑

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]);
    }
}];

您可以更改withinKilometerswithinMiles.

于 2015-05-26T07:46:07.430 回答