我在我的數據雲解析中添加了一個「位置」類型的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
}
}
}
}
它是好方法?在哪裏插入她?請幫我 謝謝
這是我給你的完全相同的答案[here](http://stackoverflow.com/questions/25923761/locations-around-the-users-with-parse/25924345#25924345)。你可以把它放在viewDidLoad中,但是一旦它執行,你就必須對它做些什麼。 – Dehli 2014-09-19 18:01:56
Plz這個函數不能在ViewDidLoad中幫助我plz – 2014-09-21 12:45:38
我有這個錯誤「屬性」geoPointForCurrentLocationBackground「找不到對象的類型」PFGeoPoint「 – 2014-09-21 12:47:42