2014-01-30 3 views
0

나는 가장 가까운 10 개의 상점 위치를 모아서 근접한 순서로 표시하기로되어있는 PFQueryTableView를 가지고 있습니다. 나는있는 tableview과 같이 조회 : 위의 코드는 잘 작동위치 Query in Parse 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]; 

그냥 빈 기본 tableview를 반환합니다. 누구든지 쿼리가 위치 행과 함께 작동하지 않는 이유가 무엇입니까?

+0

지리적으로 'userLocation'은 실제로 테스트 할 때 어디에 있는지 확인해 보셨습니까? 그 지역의 50 마일 이내에 위치가있는 'TopToday' 기록이 있습니까? –

답변

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