Question

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;
}
Was it helpful?

Solution

use the NSUserDefault method to save the user's current location latitude and longitude and use it your view. make location update manager is start not stop so you can get exactly latitude and longitude

use it when the user location update

     [[NSUserDefaults standardUserDefaults] setObject:latitude forKey:@"latitude"];
 [[NSUserDefaults standardUserDefaults] setObject:latitude forKey:@"longitude"];  

use it where you calculating distance

   NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"latitude"];
 NSString *long =  [[NSUserDefaults standardUserDefaults] objectForKey:@"longitude"];

OTHER TIPS

Fetch the user's location in the previous view and then when loading the the listview send the data to the view like this (userLocation in this case can be a JSON string with long and latitude)

- (void) loadVenuesListView {
    VenuesViewController *venuesView = [[VenuesViewController alloc] initWithNibName:@"VenuesViewController" bundle:nil];
    venuesView.userLocation = fetchedUserLocation;
    [[self navigationController] pushViewController:venuesView animated:YES];
    [venuesView release];   
}

Then in your VenuesViewController you have to make a property:

@property (nonatomic, retain) NSString *userLocation;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top