简体   繁体   中英

Retrieving users location with Parse nearGeoPoint

I have an user table and every user has own location (PFGeoPoint object). Now what is I want to do is sorting and retrieving users from nearest to farthest location. The problem is some users doesnt have any location info(latitude, longitude, geopoint). Their currentLocation column are empty. That's way query doesn't work as I expected.

I put below code to save users location to parse

    [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {

    NSLog(@"User is currently at %f, %f", geoPoint.latitude, geoPoint.longitude);
    self.userLocation = geoPoint;
    [[PFUser currentUser] setObject:geoPoint forKey:@"currentLocation"];
    [[PFUser currentUser] saveInBackground];

 }];

And put below code on my retrieve query method

PFGeoPoint *userGeoPoint = self.userLocation; //pass user location to PFGeoPoint

[query whereKey:@"currentLocation" nearGeoPoint:userGeoPoint]; //retrieve users from nearest to rarest 

I updated manually users geopoint information as latitude 0.0, longitude 0.0 if the user doesn't have geopoint info or doesn't let to app to use location. Then I can retrieve users from nearest to farthest whether if they doesn't have geoopint information. And the code that I used for it in viewDidLoad is below.

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

if(status == kCLAuthorizationStatusAuthorized ||
   status == kCLAuthorizationStatusAuthorizedAlways ||
   status == kCLAuthorizationStatusAuthorizedWhenInUse) {

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {

    NSLog(@"User is currently at %f, %f", geoPoint.latitude, geoPoint.longitude);
    self.userLocation = geoPoint;
    [[PFUser currentUser] setObject:geoPoint forKey:@"currentLocation"];
    [[PFUser currentUser] saveInBackground];

 }];
}

else if (status == kCLAuthorizationStatusNotDetermined ||
         status == kCLAuthorizationStatusRestricted ||
         status == kCLAuthorizationStatusDenied) {

    PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:0.0 longitude:0.0];

    [[PFUser currentUser] setObject:geoPoint forKey:@"currentLocation"];
    [[PFUser currentUser] saveInBackground];
}

It sounds like you need to filter out users who don't have a geopoint.

Try this:

PFGeoPoint *userGeoPoint = self.userLocation; //pass user location to PFGeoPoint

[query whereKeyExists:@"currentLocation"]; //This is the key line you are missing
[query whereKey:@"currentLocation" nearGeoPoint:userGeoPoint];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM