1

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.

Sushrita
  • 725
  • 10
  • 29

1 Answers1

2

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.

Simon Degn
  • 901
  • 1
  • 12
  • 40
  • thanks for your suggestion.I have something to ask will it return the array of users those are within this 400km radius? – Sushrita May 26 '15 at 07:52
  • No it won't. It's a query and not the objects. It's more like a description of what you want to use from the data base. You can read more on: http://parse.com/docs/ios/guide#queries. To get the objects (users) from the data base, you can use: `[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { ... }];` – Simon Degn May 26 '15 at 07:57
  • can you please tell me how to get individual distance between the currentUser and the other users? – Sushrita May 26 '15 at 08:08
  • It's in my answer above now. :) – Simon Degn May 26 '15 at 08:18