使用Parse nearGeoPoint检索用户位置

时间:2014-10-15 07:23:31

标签: ios objective-c xcode parse-platform location

我有一个用户表,每个用户都有自己的位置(PFGeoPoint对象)。现在我想要做的是从最近到最远的位置对用户进行排序和检索。问题是一些用户没有任何位置信息(纬度,经度,geopoint)。他们的currentLocation列是空的。这种方式查询不能像我预期的那样工作。

我在下面的代码中保存用户位置以进行解析

    [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];

 }];

将以下代码放在我的检索查询方法

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

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

2 个答案:

答案 0 :(得分:1)

我手动更新用户地理位置信息为纬度0.0,经度0.0如果用户没有地理位置信息或不让应用程序使用位置。然后我可以检索从最近到最远的用户,无论他们是否没有geoopint信息。我在viewDidLoad中使用的代码如下所示。

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

答案 1 :(得分:0)

听起来您需要过滤掉没有地理位置的用户。

试试这个:

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