How can I get users within a given radius of distance using PFGeoPoint

673 views Asked by At

I have PFGeoPoint in Parse and there is a serachView where the user will enter the radius in miles. So it will return a list of users who are within dat radius of distance.

I know we can fetch the distance from coordinates but I don't know how to get all the users there and check one by one.

Please if any one can suggest me something then it will be a great help for me as I am new to iOS and Parse.

1

There are 1 answers

4
Simon Degn On BEST ANSWER

EDITED

Parse actually have a simple solution to that:

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

You can change withinKilometers to withinMiles.