美文网首页
reverseGeocodeLocation在列表中刷新解决方案

reverseGeocodeLocation在列表中刷新解决方案

作者: 田陌允 | 来源:发表于2021-04-21 15:49 被阅读0次

本人iOS新手,之前都是做C++的,有更好解决方案的大神请在评论区告诉我。谢谢!
需求:列表显示查询记录定位信息(根据接口返回的经纬度信息,别问为什么不直接返回地址信息......)

存在问题:

  1. reverseGeocodeLocation返回信息后再更新列表model
    2.已获取的位置信息通过NSString缓存数组存储(只初始化一次)

代码:

#pragma mark - 只初始化一次
#define REUSEED_SIZE    50
static NSString *reUsedStr[REUSEED_SIZE] = {nil};

- (void)firstUpdateView {
    [self addJJKeyNotification];
    self.navigationItem.title = @"查询记录".localized;
    [self.tableView registerCell:[CheckCodeRecordsCell class] isHaveNib:YES];
    
    // 重置缓存数组
    for (int i = 0; i < REUSEED_SIZE; i++) {
        reUsedStr[i] = @"";
    }
}

...
...
...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CheckCodeRecordsCell";
    CheckCodeRecordsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CheckCodeRecordsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    BaseModel *model = self.dataArray[indexPath.section];
    CheckRecordModel *mm = (CheckRecordModel *)model;
    mm.indexNum = [NSString stringWithFormat:@"%ld", (long)(self.dataArray.count - indexPath.section)];

    //  已缓存过的数据
    if (reUsedStr[indexPath.section].isNotBlank) {
#ifdef JJLOG_SHOW
    JJLog(@"加载已缓存*** 第 %ld 行 Address %@", (long)(self.dataArray.count - indexPath.section), reUsedStr[indexPath.section]);
#endif
        mm.address = reUsedStr[indexPath.section];
        cell.model = mm;
    } else {//  未缓存过的数据
        __block NSString *returnAddress = @"";
        NSString *A = mm.latitude;
        NSString *B = mm.longitude;
        float la = [A floatValue];
        float lo = [B floatValue];
        CLLocation *location = [[CLLocation alloc] initWithLatitude:la longitude:lo];
        CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
        [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
            if (error) {
                if (A.isNotBlank && B.isNotBlank) {
                    mm.address = [NSString stringWithFormat:@"%@", @"正在加载地址信息...".localized];
                }
                else  // 经纬度为空时也必定会返回error 
                {
                    mm.address = [NSString stringWithFormat:@"%@", @"无地址信息...".localized];
                }
            }
            else
            {
                CLPlacemark *placemark = [placemarks lastObject];
                NSString *placemarkCountry = placemark.country.length > 0 ? placemark.country : @"";//国家暂时注释
                NSString *placemarkArea = placemark.administrativeArea.length > 0 ? placemark.administrativeArea : @"";
                NSString *placemarkLocality = placemark.locality.length > 0 ? placemark.locality : @"";
                NSString *placemarkSubLocality = placemark.subLocality.length > 0 ? placemark.subLocality : @"";
                NSString *placemarkName = placemark.name.length > 0 ? placemark.name : @"";
                NSString *lang = [AFClient getCurrentLanguage];
                if ([lang isEqualToString:@"1"]) {
                    returnAddress = [NSString stringWithFormat:@"%@%@%@%@", placemarkArea, placemarkLocality, placemarkSubLocality, placemarkName];
                } else {
                    returnAddress = [NSString stringWithFormat:@"%@ %@ %@ %@", placemarkName, placemarkSubLocality, placemarkLocality, placemarkArea];
                }
                mm.address = returnAddress;
                // 缓存到数组
                reUsedStr[indexPath.section] = [NSString stringWithFormat:@"%@", returnAddress];
            }

            cell.model = mm;
        #ifdef JJLOG_SHOW
            JJLog(@"第 %ld 行 returnAddress %@", (long)(self.dataArray.count - indexPath.section), reUsedStr[indexPath.section]);
        #endif
        }];
    }

    return cell;
}

注释应该挺清楚了

相关文章

网友评论

      本文标题:reverseGeocodeLocation在列表中刷新解决方案

      本文链接:https://www.haomeiwen.com/subject/dakzlltx.html