-4

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..

MCKapur
  • 9,127
  • 9
  • 58
  • 101
  • 1
    The second line has several syntax errors and erroneous assumptions about a string literal's behavior. –  Jan 26 '13 at 06:31
  • Ya i know..i tried changing it but it always gave me the same error...even the first line is giving me the error.can you please tell me the correct way? – user2013118 Jan 26 '13 at 06:40

1 Answers1

0

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];

MCKapur
  • 9,127
  • 9
  • 58
  • 101
  • Thanks...the first one helped...that was so stupid of me...and now it shows -1.00000 as the distance in every entry in the table. and yep my plist is an array of dictionaries...each restaurant a dictionary. – user2013118 Jan 26 '13 at 07:12
  • Thats fine. If the answer helped click the green tick silhouete below the upvote tools to accept my answer. – MCKapur Jan 26 '13 at 07:15