【问题标题】:Retrieving users location with Parse nearGeoPoint使用 Parse nearGeoPoint 检索用户位置
【发布时间】:2014-10-15 07:23:31
【问题描述】:

我有一个用户表,每个用户都有自己的位置(PFGEoPoint 对象)。现在我想做的是从最近到最远的位置对用户进行排序和检索。问题是一些用户没有任何位置信息(纬度、经度、地理点)。他们的 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 

【问题讨论】:

  • 有什么问题?它没有返回任何东西,是否返回不准确或不正确的结果?请在您的问题中添加对不起作用的确切说明。
  • 问题是我在(解析)_users 表上有 15 个用户。有些用户有地理点,有些用户有。我想首先带来具有地理点信息的用户,并将它们从最近到最远排序。但是查询不会带来没有地理点信息的用户。

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


【解决方案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];
}

【讨论】:

    【解决方案2】:

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

    试试这个:

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

    【讨论】:

    • 您好 Ryan,感谢您的回复,但我还需要用户没有 currentLocation。首先,我将检索具有地理点信息的用户,并从最近到最远对其进行排序,然后我将检索其他没有地理点信息的用户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多