I have add a column "Location" type GeoPoint in my data cloud Parse . I use this method to get the current position of user : `
- (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];
}
}];
}
I have this method to get position of Restaurants Location ( in my Data Cloud column Location ) :
- (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;
}
Now I would want to know how how to calculate distance enters the position's user and those of restaurants .
I have this method but I don't know where i can insert this method
//Get the user's current location
PFGeoPoint.geoPointForCurrentLocationInBackground {
(point:PFGeoPoint!, error:NSError!) -> Void in
if error == nil {
//Point contains the user's current point
//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
}
}
}
}
it is the good method ? and where insert her ? Please help me Thanks