I have an app that lists a number of venues which have their latitude and longitudes stored in database. I populate the list with json. And then I display the result in a dynamic table. In the cells, I have distance in kilometers of the venue. I find this distance by getting users current location and calculating the distance with the venues coordinates.
It works fine but my problem is as the view loads, the distances are wrong until after 3-5 seconds when the user location is updated, it shows correct distances.
I get the user location and populate the cells in viewdidload.
My question is: is it possible to get the user's location in the previous view and pass it onto this view so that the distances shown are correct initially?
Here is the code:
I first get the user loc in viewdidload:
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
Then further than I get my json data, etc. Then I do the tableView:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rows count];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// int degrees = newLocation.coordinate.latitude;
// double decimal = fabs(newLocation.coordinate.latitude - degrees);
// int minutes = decimal * 60;
// double seconds = decimal * 3600 - minutes * 60;
// NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
// degrees, minutes, seconds];
NSString *lat = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
latLabel.text = lat;
// degrees = newLocation.coordinate.longitude;
// decimal = fabs(newLocation.coordinate.longitude - degrees);
// minutes = decimal * 60;
// seconds = decimal * 3600 - minutes * 60;
// NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
// degrees, minutes, seconds];
NSString *longt = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
longLabel.text = longt;
//CGFloat Alat = [lat floatValue];
//CGFloat Alon = [longt floatValue];
self.aalat = lat;
self.aalon = longt;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell.
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
THELATITUDE = [dict objectForKey:@"L_LAT"];
THELONGITUDE = [dict objectForKey:@"L_LON"];
CGFloat Nlat = [THELATITUDE floatValue];
CGFloat Nlon = [THELONGITUDE floatValue];
CGFloat NNlat = [aalat floatValue];
CGFloat NNlon = [aalon floatValue];
// NSLog(@"lat1: %f", Nlat);
// NSLog(@"Lon1: %f", Nlon);
// NSLog(@"lat2: %f", NNlat);
// NSLog(@"Lon2: %f", NNlon);
// NSLog(@"aalat: %@", aalat);
// NSLog(@"aalon: %@", aalon);
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:Nlat longitude:Nlon];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:NNlat longitude:NNlon];
// NSLog(@"Distance i meters: %f", [location1 getDistanceFrom:location2]);
float mesafe1 = [location1 getDistanceFrom:location2 ] /1000;
int x1 = mesafe1 * 100.0;
float mesafe2 = (float) x1 / 100.0;
//Create a string.
NSString *strOne = [[NSString alloc] init];
NSString *strTwo = [[NSString alloc] init];
NSString *strThree = [[NSString alloc] init];
NSString *strFour = [[NSString alloc] init];
NSString *strFive = [[NSString alloc] init];
//Append strings.
strOne = @"MESAFE: ";
strTwo = [NSString stringWithFormat:@"%0g", mesafe2];
strThree = @" kilometre";
strFour = [strOne stringByAppendingString:strTwo];
strFive = [strFour stringByAppendingString:strThree];
NSString *mesafe = strFive;
cell.textLabel.text = [dict objectForKey:@"L_NAME"];
//cell.detailTextLabel.text = [dict objectForKey:@"LISTING_ID"];
cell.detailTextLabel.text = mesafe;
[cell.textLabel setTextColor:[UIColor blackColor]];
return cell;
}