Location Query in Parse TableView

偶尔善良 提交于 2019-12-24 22:25:28

问题


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

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

It just returns a blank default tableview. Does anyone have any thoughts I why the query does not work with the location line?


回答1:


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;

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

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


来源:https://stackoverflow.com/questions/21445979/location-query-in-parse-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!