Question

I have a long list of places with Latitudes and Longitudes in a plist. I want to show a tableView of the places only within X distance of the user's current location. Is there a way to create objects from the lats & longs in the plist file so I can use 'distanceFromLocation'? More importantly, how do I get the array to only display the names with a distance from current less than X? I'm assuming I would need to make a series of objects from lats & longs in the plist, then do an objects in array if objects distanceFrom is less than X, correct?

Please help.

Here's where I am now: I get an error on the double clubLatitude line

- (void)viewDidLoad {
 [super viewDidLoad];

     NSArray *clubArray = [NSArray arrayWithObjects:[self danceClubLocation], nil];
     self.tableData = clubArray;
}

-(CLLocation *)danceClubLocation
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];

    NSEnumerator *e = [array objectEnumerator];
    id object;
    while ((object = [e nextObject])) {
        double clubLatitude = [[array valueForKey:@"Latitude"] doubleValue];
        double clubLongitude = [[array valueForKey:@"Longitude"] doubleValue];
        CLLocation *clubLocation = [[CLLocation alloc] initWithLatitude:clubLatitude longitude:clubLongitude];
        if ([clubLocation distanceFromLocation:myLocation]<=50) {
            return clubLocation;
        }
        else return nil;
    }
    return nil;
}

-(CLLocation *)myLocation
{
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];
    NSNumber *myLatitude = [NSNumber numberWithDouble:coordinate.latitude];
    NSNumber *myLongitude = [NSNumber numberWithDouble:coordinate.longitude];
    double myLatitudeD = [myLatitude doubleValue];
    double myLongitudeD = [myLongitude doubleValue];
    myLocation = [[CLLocation alloc]initWithLatitude:myLatitudeD longitude:myLongitudeD];
    return myLocation;
}
Was it helpful?

Solution

As @DavidNeiss said, you have to iterate over the list (an NSArray with the plist as source) and it would be something like this:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"latlong" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];

NSEnumerator *e = [array objectEnumerator];
id object;
while (object = [e nextObject]) {
  // do something with object
}

Then you can do what you want (removing what's far from the user or whatever).

OTHER TIPS

Read in your plist, iterate over it to pull out ones within X distance and populate any array with them that will be the data source for your table view?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top