解析表维图中的位置查询



我有一个pfquerytableview,应该收集10个最接近的商店位置,并按近距离显示它们。我像这样询问桌面视图:

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

它只是返回一个空白默认tableView。有人有任何想法吗?

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

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

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

,最后在查询中引用当前点。

- (PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:@"TopToday"];
    query.limit = 7;
    [query whereKey:@"location" nearGeoPoint:self.currentGeoPoint withinMiles:50];
    return query;
}

最新更新