iOS:当数据来自网络服务作为响应时,如何获取多个经度和纬度的位置名称?

标签 ios objective-c xcode google-maps reverse-geocoding

我需要帮助我的代码。我正在获取经度和纬度作为网络服务的响应。我需要将它们转换为位置名称并将其显示在相应的表格 View 单元格标签中。
以下是我的 View 的响应、代码和屏幕截图,其中我显示了我转换后的城市、国家/地区名称,并在我出错的地方更正了我的代码。

enter image description here

enter image description here

enter image description here

#import <UIKit/UIKit.h>
#import "JobTableViewCell.h"
#import "SlideNavigationController.h"
#import <CoreLocation/CoreLocation.h>

@interface JobPostedViewController : UIViewController <SlideNavigationControllerDelegate, UITableViewDataSource, CLLocationManagerDelegate>

@property (strong, nonatomic) NSArray* job_id;
@property (strong, nonatomic) NSArray* assigned_user_id;
@property (strong, nonatomic) NSArray* job_title;
@property (strong, nonatomic) NSArray* job_description;
@property (strong, nonatomic) NSArray* job_priority;
@property (strong, nonatomic) NSString* job_longitude;
@property (strong, nonatomic) NSArray* date;
@property (strong, nonatomic) NSString* job_latitude;
@property (strong, nonatomic) NSArray* job_completed;
@property (strong, nonatomic) NSArray* job_start_date;
@property (strong, nonatomic) NSArray* bids;

@property (nonatomic, strong) CLGeocoder *myGeocoder;

- (IBAction)onClickJobButton:(id)sender;


@property (weak, nonatomic) IBOutlet UIButton *sideNavigation;
@property (weak, nonatomic) IBOutlet UITableView *jobPostedTableView;

@end
`

    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.jobPostedTableView.dataSource = self;

    //Slide Navigation
    [self.sideNavigation addTarget:[SlideNavigationController sharedInstance] action:@selector(toggleLeftMenu) forControlEvents:UIControlEventTouchUpInside];

    WebManager *manager = [WebManager sharedInstance];
    [manager getJobPostedWithCompletionBlock:^(id response){
        NSDictionary *dictionary = (NSDictionary *)response;

        // Read data from JSON
        NSDictionary *responseObject = [dictionary objectForKey:@"response"];
        NSLog(@"The Array%@",responseObject);

        self.bids = [responseObject objectForKey:@"bids"];
        self.job_description = [responseObject objectForKey:@"job_description"];
        self.job_id = [responseObject objectForKey:@"job_id"];
        self.job_completed = [responseObject objectForKey:@"job_completed"];
        self.job_latitude = [responseObject objectForKey:@"job_latitude"];
        self.job_longitude = [responseObject objectForKey:@"job_longitude"];
        self.job_priority = [responseObject objectForKey:@"job_priority"];
        self.job_start_date = [responseObject objectForKey:@"job_start_date"];
        self.job_title = [responseObject objectForKey:@"job_title"];

        [self.jobPostedTableView reloadData];
    }];

        CLGeocoder *ceo = [[CLGeocoder alloc]init];
        CLLocation *loc = [[CLLocation alloc]initWithLatitude:[self.job_latitude floatValue] longitude:[self.job_longitude floatValue]]; //insert your coordinates

        [ceo reverseGeocodeLocation:loc
                  completionHandler:^(NSArray *placemarks, NSError *error) {
                      CLPlacemark *placemark = [placemarks objectAtIndex:0];
                      NSLog(@"placemark %@",placemark);
                      //String to hold address
                      [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                      NSLog(@"addressDictionary %@", placemark.addressDictionary);
                      NSArray *ar = [placemark.addressDictionary objectForKey:@"locality"];
                      NSLog(@"placemark %@",ar); // Extract the city name
                      NSLog(@"placemark %@",placemark.country);  // Give Country Name

                  }

         ];
    }


#pragma mark - SlideNavigationController Methods

- (BOOL)slideNavigationControllerShouldDisplayLeftMenu
{

    return YES;
}

#pragma mark - TableView Data Source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.job_title count];
}

- (NSInteger) numberOfSectionsInTableview:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"JobTableViewCell";
    JobTableViewCell *cell = (JobTableViewCell *)[self.jobPostedTableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil) {

        cell = [[JobTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    // Comparing array string to display an urgent image
    if ([[self.job_priority objectAtIndex:indexPath.row]
         isEqualToString:@"urgent"]) {
        cell.urgentLabel.hidden = NO;
    } else {
            cell.urgentLabel.hidden = YES;
    }
    // Condition whether job completed is open or closed
    if ([[self.job_completed objectAtIndex:indexPath.row]
         isEqualToString:@"1"]) {
        cell.jobStatus.text = @"Open";
        [cell.jobStatus setTextColor:[UIColor colorWithRed:(84/255.f) green:(56/255.f) blue:(255/255.f) alpha:1.0f]];
        cell.flagImage.image = [UIImage imageNamed:@"jobPosted_opened.PNG"];
    } else {
        cell.jobStatus.text = @"Closed";
        [cell.jobStatus setTextColor:[UIColor colorWithRed:(179/255.f) green:(179/255.f) blue:(180/255.f) alpha:1.0f]];
        cell.flagImage.image = [UIImage imageNamed:@"jobPosted_closed.PNG"];
    }


    cell.jobTitle.text = [self.job_title objectAtIndex:indexPath.row];
    cell.jobContent.text = [self.job_description objectAtIndex:indexPath.row];
    cell.bidsLabel.text = [[self.bids objectAtIndex:indexPath.row ]stringValue];
    return cell;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

最佳答案

解决方法如下:

  1. 将这些属性NSString更改为NSArray:

    @property (strong, nonatomic) NSString* job_longitude;
    @property (strong, nonatomic) NSString* job_latitude;
    

  1. 添加一个新的属性来保存地理编码结果:

    @property (strong, nonatomic) NSMutableDictionary* reversedGeocodes;
    

  1. viewDidLoad:方法中设置reversedGeocodes:

    self.reversedGeocodes = [NSMutableDictionary dictionary];
    

  1. 添加此方法以获取反向地理代码(并保存以备将来使用):

    - (void)locationNameWithLat:(float)lat
                            lng:(float)lng
              completionHandler:(void (^)(NSString *locationName, NSError *error))completion
    {
        if (!lat || !lng)
            completion(@"Unknown location", nil);
    
        if (!completion || !lat || !lng)
            return;
    
        NSString *latStr = @(lat).description;
        NSString *lngStr = @(lng).description;
    
        // For each pair of lat and lng creating a unique key and saving the result
        // so we don't have to process the same pair multiple times.
        // Example:
        // lat = 23.039567; lng = 72.566004;
        // key = @"23.03956772.566004"; value would be whatever CoreLocation returns
        NSString *key = [latStr stringByAppendingString:lngStr];
    
        if (key.length) {
            if ([self.reversedGeocodes[key] length]) {
                completion (self.reversedGeocodes[key], nil);
            } else {
                self.reversedGeocodes[key] = @"Loading...";
                CLGeocoder *geocoder = [[CLGeocoder alloc] init];
                CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
    
                [geocoder reverseGeocodeLocation:location
                               completionHandler:^(NSArray *placemarks, NSError *error) {
                                   if (error) {
                                       completion(@"Unknown location", error);
                                   } else {
                                       CLPlacemark *placemark = placemarks.firstObject;
                                       NSString *locationName = placemark.country; // Default = country
    
                                       if (placemark.locality.length) // add city if available
                                           locationName = [NSString stringWithFormat:@"%@, %@", placemark.locality, placemark.country];
    
                                       self.reversedGeocodes[key] = locationName.length ? locationName : @"Unknown location";
    
                                       completion(self.reversedGeocodes[key], nil);
                                   }
                               }
                 ];
            }
        } else {
            completion(@"Unknown location", nil);
        }
    }
    

  1. 在返回 cell 之前从 cellForRowAtIndexPat: 方法调用上述方法:

    float lat = self.job_latitude[indexPath.row];
    float lng = self.job_longitude[indexPath.row];
    
    [self locationNameWithLat:lat
                          lng:lng
            completionHandler:^(NSString *locationName, NSError *error) {
                cell.locationName.text = locationName;
            }];
    
    return cell;
    

附言让我知道是否因为我在这里输入了一些代码而遗漏了任何内容或拼写错误...

关于iOS:当数据来自网络服务作为响应时,如何获取多个经度和纬度的位置名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31488260/

相关文章:

ios - WatchKit,AttributedString 格式不起作用

ios - 如何释放不再使用的 UIImage 内存

objective-c - 我可以更改 Spotlight 搜索结果中显示的名称吗?

ios - XCode 7.3 和 Swift 2.2 中没有这样的模块 'Alamofire'

iphone - 字体文件已添加到项目、plist、代码和编译资源中,但仍然无法正常工作

ios - Storyboard - 在没有 UINavigationController 的情况下弹出到根 UIViewController?

ios - PrepareForSegue 对于我的其他函数来说太快了,变量没有及时更新?

ios - 为什么这个获取请求没有返回任何内容

iphone - 如何从单 View 应用程序启动 cocos2D 游戏

ios - 在 Objective-C 中调试简单的函数