Search code examples
objective-ccllocationmanagercllocationcllocationdistance

Trying to get distance calculated from gps detected location to a number of restaurants in a plist file


I have to do a project in which I have to calculate the distance from my location which is detected by gps to a number of restaurants whose locations are in a plist file and then display the distance in a list view as a subtitle of the restaurant name.

I know about the CLLocationDistance and I wrote one function which is :-

-(double)distanceBetPoints
{
    CLLocation *user = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];
    CLLocationDistance distance = [user distanceFromLocation:restaurant.location];
    return distance;
}

Here location manager is determining my location and restaurant.location is the location entry in the plist file.

I have written the following in the cell configuration :-

NSString *d = [NSString stringWithFormat:@"%@",[mapVC.distanceBetPoints]];
cell.detailTextLabel.text = [@"%@",[d]];

Here mapVC is my object of mapVC class where the method for distance calculation exists, both these lines are giving me an error for expected identifier.

Also how do I calculate it separately for every restaurant and show it in tableview? do I have to store the values in an array or something? If yes than where should I put my method and where should I call it so that it gets calculated for every restaurant and stored in an array..


Solution

  • Ummm.... I think this is your error:

    NSString *d = [NSString stringWithFormat:@"%@",[mapVC.distanceBetPoints]];
    cell.detailTextLabel.text = [@"%@",[d]];
    

    Shouldn't it be this:

    NSString *d = [NSString stringWithFormat: @"%@", mapVC.distanceBetPoints];
    cell.detailTextLabel.text = d;
    

    I'm not sure, the answer seems so simple and obvious here that I just might not have ever seen this syntax and I'm assuming its incorrect, I've never seen so many brackets and that formatting!

    Also how do I calculate it separately for every restaurant and show it in tableview? do I have to store the values in an array or something? If yes than where should I put my method and where should I call it so that it gets calculated for every restaurant and stored in an array..

    Store it into a local array, and use a method (probably should put it in viewDidLoad) to populate the array with the restaurants. I'm assuming your plist is an array of dictionaries (each dictionary is a restaurant). But if not, what is your plist of restaurants? An array of strings or what?

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyPlistName" ofType:@"plist"];
    

    restaurantArray = [[NSArray alloc] initWithContentsOfFile: plistPath];