0

我有一个 PFQueryTableView,它应该收集 10 个最近的商店位置并按接近顺​​序显示它们。我像这样查询tableview:

    - (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:@"TopToday"];
    query.limit = 7;
    CLLocation *currentLocation = locationManager.location;
    PFGeoPoint *userLocation =
    [PFGeoPoint geoPointWithLatitude:currentLocation.coordinate.latitude
                           longitude:currentLocation.coordinate.longitude];

    return query;
}

上面的代码工作正常,只是收集了 7 个随机位置,没有特定的顺序。但是,当我添加这一行时:

[query whereKey:@"location" nearGeoPoint:userLocation withinMiles:50];

它只是返回一个空白的默认表格视图。有没有人有任何想法我为什么查询不适用于定位线?

4

1 回答 1

0

我的猜测是在您的位置管理器返回有效位置之前运行查询。

我会为当前地理点创建一个新属性;

@property (nonatomic, strong) PFGeoPoint *currentGeoPoint;

然后覆盖 loadObjects 以确保地理点在查询运行之前确实存在。

- (void)loadObjects
{
    if (!self.currentGeoPoint)
    {
        [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geo, NSError *error)
         {
             self.currentGeoPoint = geo;
             [super loadObjects];
         }];
    }
    else
    {
        [super loadObjects];
    }
}

最后在您的查询中引用 currentGepoint。

- (PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:@"TopToday"];
    query.limit = 7;
    [query whereKey:@"location" nearGeoPoint:self.currentGeoPoint withinMiles:50];
    return query;
}
于 2014-02-18T19:54:47.587 回答