Hey Everyone,
I've been developing an application for about a month now and am looking for some help accomplishing the following scenario:
The user will select a cell from a starting UITableView which will then load a new UITableView. Once that selection occurs, I need to look up the user's latitude/longitude, hand that information to my server by embedding it in a URL, and then load the returned XML information in that new UITableView.
The problem that I am having is that the CLLocationManager doesn't seem return coordinates instantly enough for the tableview to populate with that data - here is some sample code:
CLController.m file:
Code:
@implementation CLController
@synthesize locationManager, delegate;
- (id) init{
self = [super init];
if (self != nil){
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
}
return self;
}
- (void)locationManager:(CLLocationManager *) manager didUpdateToLocation: (CLLocation *)
newLocation fromLocation: (CLLocation *) oldLocation{
[self.delegate locationUpdate:newLocation];
}
- (void)locationManager:(CLLocationManager *) manager didFailWithError: (NSError *) error{
[self.delegate locationError:error];
}
- (void) dealloc{
[self.locationManager release];
[super dealloc];
}
myTableView.m file, viewDidLoad method:
Code:
- (void)viewDidLoad {
[super viewDidLoad];
locationController = [[CLController alloc] init];
locationController.delegate = self;
[locationController.locationManager startUpdatingLocation];
}
myTableView.m file, locationUpdate method:
Code:
- (void)locationUpdate:(CLLocation *)location{
CLLocationCoordinate2D loc = [location coordinate];
lat = [[NSString alloc] initWithFormat:@"%f", loc.latitude];
lng = [[NSString alloc] initWithFormat:@"%f", loc.longitude];
NSLog(@"TableView lat= %@", lat);
NSLog(@"TableView lng= %@", lng);
[locationController.locationManager stopUpdatingLocation];
NSMutableString *myURL = [[NSMutableString alloc] initWithString:@"http://mywebsite/somecode.php?lat="];
//NSLog(@"Beginning myTableView XML Parsing");
[myURL appendString: lat];
[myURL appendString: @"&lng="];
[myURL appendString: lng];
[myURL appendString: @"&radius=10"];
//Here I have various logic to parse the XML and populate an array.
//In my tableview, numberOfRowsInSection method, it checks this
//array to return the proper number of cells, which is always returning 0
//despite the array containing data
[myTableView reloadData];
[myURL release];
}
As a side note, I am actually seeing the lat and lng values in my Console, so I know that the method is getting called, but it isn't being called in time for the tableView methods to actually use the data in the method. And [myTableView reloadData] doesn't seem to have any effect.
How can I force myTableView to wait for the locationUpdate before it actually populates the cells? Is there a better way to do all this?