issue passing geolocation coords to UITableViewCell

85 views Asked by At

first i made a NSString with the address i need

NSString *bldgAddress = [NSString stringWithFormat:@"%@ %@ %@", listingsDisplayed.ADDRESS1, listingsDisplayed.TOWN, listingsDisplayed.ZIPCODE];

then i called LMGeocoder Github Link

[[LMGeocoder sharedInstance] geocodeAddressString:bldgAddress service:kLMGeocoderGoogleService completionHandler:^(LMAddress *address , NSError *error) {
 if (address && !error) {
 _latitude = [NSString stringWithFormat:@"%f", address.coordinate.latitude];
 _longitude = [NSString stringWithFormat:@"%f", address.coordinate.longitude];
 } else {
 NSLog(@"%@", error);
 }}];

it works fine up to here, _lat and _long are declared in my .h file and the values are assigned respectively

cell.lat.text = [NSString stringWithFormat:@"%@,%@", _latitude, _longitude];

issue start when i try to load that data into the cell, it shows (null),(null). i dont understand why the values disappear, any help would be greatly appreciated


Edit: Code that worked thanks to @dhanush

    __block UILabel *currentLAbel = cell.lat;
[[LMGeocoder sharedInstance] geocodeAddressString:bldgAddress service:kLMGeocoderGoogleService completionHandler:^(LMAddress *address , NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{

        if (address && !error) {
            listingsDisplayed.Latitude = [NSString stringWithFormat:@"%f", address.coordinate.latitude];
            listingsDisplayed.Longitude = [NSString stringWithFormat:@"%f", address.coordinate.longitude];

            currentLAbel.text = [NSString stringWithFormat:@"%@,%@", listingsDisplayed.Latitude, listingsDisplayed.Longitude];
            cell.lat = currentLAbel;

        } else {
            NSLog(@"%@", error);
        }
    });
}];

2

There are 2 answers

1
Manikandan On BEST ANSWER

Try This

    UILabel *currentLAbel = cell.lat;
    [[LMGeocoder sharedInstance] geocodeAddressString:bldgAddress service:kLMGeocoderGoogleService completionHandler:^(LMAddress *address , NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{

    if (address && !error) {
        listingsDisplayed.Latitude = [NSString stringWithFormat:@"%f", address.coordinate.latitude];
        listingsDisplayed.Longitude = [NSString stringWithFormat:@"%f", address.coordinate.longitude];

        currentLAbel = [NSString stringWithFormat:@"%@,%@", listingsDisplayed.Latitude, listingsDisplayed.Longitude];
        cell.lat = currentLabel;

    } else {
        NSLog(@"%@", error);
    }
     });
  }];             

You should always do UI Changes in Main thread. Initially Those values are null. Once you got those values you can set it inside the block. CurrentLabel will help to get the particular label if you are scrolling the table. try with cell.lat. if it is not working use the current label.

2
Mahendra On

You are getting lat & long in block...that is asynchronous so that is not executed on main thread.

You set the lat & long value before you get its value from block. That's why you are getting null value.

You put breakpoint and track flow of execution, you will get the idea.

Good luck !! :)