2014-09-19 58 views
0

我在我的數據雲解析中添加了一個「位置」類型的GeoPoint。 我用這個方法來獲取用戶當前位置:`地理定位解析

- (IBAction)insertCurrentLocation:(id)sender { 
// If it's not possible to get a location, then return. 
CLLocation *location = self.locationManager.location; 
if (!location) { 
    return; 
} 

// Configure the new event with information from the location. 
CLLocationCoordinate2D coordinate = [location coordinate]; 
PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude]; 
PFObject *object = [PFObject objectWithClassName:@"Recipe"]; 
[object setObject:geoPoint forKey:@"Location"]; 

[object saveEventually:^(BOOL succeeded, NSError *error) { 
    if (succeeded) { 
     // Reload the PFQueryTableViewController 
     [self loadObjects]; 
    } 
}]; 

}

我有這樣的方法來獲得(在我的數據雲柱位置)餐廳地點的位置:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { 
// A date formatter for the creation date. 
static NSDateFormatter *dateFormatter = nil; 
if (dateFormatter == nil) { 
    dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.timeStyle = NSDateFormatterMediumStyle; 
    dateFormatter.dateStyle = NSDateFormatterMediumStyle; 
} 

static NSNumberFormatter *numberFormatter = nil; 
if (numberFormatter == nil) { 
    numberFormatter = [[NSNumberFormatter alloc] init]; 
    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle; 
    numberFormatter.maximumFractionDigits = 3; 
} 

PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"LocationCell"]; 

// Configure the cell 
PFGeoPoint *geoPoint = object[@"Location"]; 

cell.textLabel.text = [dateFormatter stringFromDate:object.updatedAt]; 

NSString *string = [NSString stringWithFormat:@"%@, %@", 
        [numberFormatter stringFromNumber:[NSNumber numberWithDouble:geoPoint.latitude]], 
        [numberFormatter stringFromNumber:[NSNumber numberWithDouble:geoPoint.longitude]]]; 

cell.detailTextLabel.text = string; 

return cell; 

}

現在我想知道如何計算距離進入位置的用戶和餐館的用戶。

我有這樣的方法,但我不知道在哪裏可以插入此方法

//Get the user's current location 
PFGeoPoint.geoPointForCurrentLocationInBackground { 
(point:PFGeoPoint!, error:NSError!) -> Void in 
if error == nil { 

//點包含用戶的當前點

//Get a max of 100 of the restaurants that are within 5km, 
    //ordered from nearest to furthest 
    var query = PFQuery(className: "restaurants") 
    query.limit = 100 
    query.whereKey("location", nearGeoPoint: point, withinKilometers: 5.0) 
    query.findObjectsInBackgroundWithBlock{ 
     (objects: [AnyObject]!, error: NSError!) -> Void in 
     if (error == nil) { 
      //objects contains the restaurants 
     } 
    } 
} 

}

它是好方法?在哪裏插入她?請幫我 謝謝

+0

這是我給你的完全相同的答案[here](http://stackoverflow.com/questions/25923761/locations-around-the-users-with-parse/25924345#25924345)。你可以把它放在viewDidLoad中,但是一旦它執行,你就必須對它做些什麼。 – Dehli 2014-09-19 18:01:56

+0

Plz這個函數不能在ViewDidLoad中幫助我plz – 2014-09-21 12:45:38

+0

我有這個錯誤「屬性」geoPointForCurrentLocationBackground「找不到對象的類型」PFGeoPoint「 – 2014-09-21 12:47:42

回答

0

我給你的功能here可以去viewDidLoad:。您找不到該物業的原因是您必須爲Info.plist添加一個密鑰,以便您的手機知道它需要了解您的位置。

在您的Info.plist中,添加一行以下值NSLocationWhenInUseUsageDescription。然後在右側給它一個說明,說:「爲了使應用程序工作,我們需要知道你的位置」。現在你可以使用geoPointForCurrentLocationInBackground

+0

如果這有效,請務必檢查我的答案是否正確。 – Dehli 2014-09-21 15:17:59