问题描述
我有一个 PFQueryTableView,它应该收集 10 个最近的商店位置并按距离顺序显示它们.我像这样查询 tableview:
I have a PFQueryTableView which is supposed to gather the 10 closest store locations and display them in order of proximity. I query the tableview like so:
- (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 个随机位置.但是,当我添加这一行时:
The above code works fine, just gathers 7 random locations in no particular order. However, when I add this line:
[query whereKey:@"location" nearGeoPoint:userLocation withinMiles:50];
它只返回一个空白的默认tableview.有没有人有任何想法我为什么查询不适用于位置线?
It just returns a blank default tableview. Does anyone have any thoughts I why the query does not work with the location line?
推荐答案
我的猜测是查询正在您的位置管理器返回有效位置之前运行.
My guess is that the query is being run before your location manager returns a valid location.
我会为当前地理点创建一个新属性;
I'd create a new property for the current geopoint;
@property (nonatomic, strong) PFGeoPoint *currentGeoPoint;
然后覆盖 loadObjects 以确保在查询运行之前地理点确实存在.
Then override loadObjects to make sure the geo point actually exists before the query runs.
- (void)loadObjects
{
if (!self.currentGeoPoint)
{
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geo, NSError *error)
{
self.currentGeoPoint = geo;
[super loadObjects];
}];
}
else
{
[super loadObjects];
}
}
最后在查询中引用 currentGepoint.
And finally reference the currentGepoint in your query.
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:@"TopToday"];
query.limit = 7;
[query whereKey:@"location" nearGeoPoint:self.currentGeoPoint withinMiles:50];
return query;
}
这篇关于Parse TableView 中的位置查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!