0

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

  • This is the exact same answer I gave you [here](http://stackoverflow.com/questions/25923761/locations-around-the-users-with-parse/25924345#25924345). You can put it inside of viewDidLoad, but you'll have to do something with objects once it executes. – Dehli Sep 19 '14 at 18:01
  • Plz this function not work in the ViewDidLoad help me plz – Mickael Zana Sep 21 '14 at 12:45
  • I have this error " Property "geoPointForCurrentLocationBackground" not found on object of type "PFGeoPoint" – Mickael Zana Sep 21 '14 at 12:47

1 Answers1

0

The function I gave you here can go in viewDidLoad:. The reason you're getting that the property is not found is that you have to add a key to Info.plist so that your phone knows it needs to know your location.

In your Info.plist, add a row with the following value NSLocationWhenInUseUsageDescription. Then on the right side give it a description saying, "In order for the app to work, we need to know your location". Now you can use geoPointForCurrentLocationInBackground.

Community
  • 1
  • 1
Dehli
  • 5,950
  • 5
  • 29
  • 44