iOS:作为响应,当数据来自web服务时,如何获取多个经度和纬度的位置名称?
我需要帮助我的代码。我从一个web服务得到了经度和纬度作为响应。我需要将它们转换为位置名称,并在相应的表视图单元格标签中显示。iOS:作为响应,当数据来自web服务时,如何获取多个经度和纬度的位置名称?,ios,objective-c,xcode,google-maps,reverse-geocoding,Ios,Objective C,Xcode,Google Maps,Reverse Geocoding,我需要帮助我的代码。我从一个web服务得到了经度和纬度作为响应。我需要将它们转换为位置名称,并在相应的表视图单元格标签中显示。 以下是我视图的响应、代码和屏幕截图,其中显示了转换后的城市、国家名称,并更正了错误代码 #import <UIKit/UIKit.h> #import "JobTableViewCell.h" #import "SlideNavigationController.h" #import <CoreLocation/CoreLocation.h>
以下是我视图的响应、代码和屏幕截图,其中显示了转换后的城市、国家名称,并更正了错误代码
#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.
}
#导入
#导入“JobTableViewCell.h”
#导入“SlideNavigationController.h”
#进口
@接口JobPostedViewController:UIViewController
@属性(强,非原子)NSArray*job_id;
@属性(强,非原子)NSArray*分配的用户id;
@财产(强、非原子)NSArray*职务;
@属性(强、非原子)NSArray*工作描述;
@属性(强、非原子)NSArray*作业优先级;
@属性(强,非原子)NSString*作业经度;
@属性(强,非原子)NSArray*日期;
@属性(强,非原子)NSString*job_纬度;
@属性(强、非原子)NSArray*作业完成;
@属性(强,非原子)NSArray*作业开始日期;
@不动产(强、非原子)NSArray*投标;
@属性(非原子,强)CLGeocoder*myGeocoder;
-(iAction)onClickJobButton:(id)发送方;
@属性(弱,非原子)IBUIButton*侧导航;
@属性(弱、非原子)IBUITableView*jobPostedTableView;
@结束
`
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后执行任何其他设置。
self.jobPostedTableView.dataSource=self;
//幻灯片导航
[self.sideNavigation addTarget:[SlideNavigationController sharedInstance]操作:@selector(toggleLeftMenu)for ControlEvents:UIControlEventTouchUpInside];
WebManager*manager=[WebManager sharedInstance];
[manager getJobPostedWithCompletionBlock:^(id响应){
NSDictionary*dictionary=(NSDictionary*)响应;
//从JSON读取数据
NSDictionary*responseObject=[dictionary objectForKey:@“response”];
NSLog(@“数组%@”,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_longide=[responseObject objectForKey:@“job_longide”];
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重载数据];
}];
CLGeocoder*ceo=[[CLGeocoder alloc]init];
CLLocation*loc=[[CLLocation alloc]initWithLatitude:[self.job\u lations floatValue]经度:[self.job\u lations floatValue]];//插入坐标
[首席执行官撤销任命:loc
completionHandler:^(NSArray*放置标记,NSError*错误){
CLPlacemark*placemark=[placemarks objectAtIndex:0];
NSLog(@“placemark%@”,placemark);
//保存地址的字符串
[[placemark.addressDictionary值forkey:@“FormattedAddressLines”]组件通过字符串连接:@“,”;
NSLog(@“addressDictionary%@”,placemark.addressDictionary);
NSArray*ar=[placemark.addressDictionary objectForKey:@“locality”];
NSLog(@“placemark%@”,ar);//提取城市名称
NSLog(@“placemark%@”,placemark.country);//给出国家名称
}
];
}
#pragma标记-SlideNavigationController方法
-(BOOL)滑动激活控制器应显示左侧菜单
{
返回YES;
}
#pragma标记-TableView数据源
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[self.job_title count];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellIdentifier=@“JobTableViewCell”;
JobTableViewCell*单元格=(JobTableViewCell*)[self.jobPostedTableView出列重用CellWithIdentifier:cellIdentifier];
如果(单元格==nil){
cell=[[JobTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellIdentifier];
}
//比较数组字符串以显示紧急图像
if([[self.job\u priority objectAtIndex:indexPath.row]
isEqualToString:@“紧急”]){
cell.urgentlab.hidden=否;
}否则{
cell.urgentlab.hidden=是;
}
//条件作业完成是打开还是关闭
if([[self.job_completed objectAtIndex:indexPath.row]
isEqualToString:@“1”]){
cell.jobStatus.text=@“打开”;
[cell.jobStatus setTextColor:[UIColor color with red:(84/255.f)green:(56/255.f)blue:(255/255.f)alpha:1.0f];
cell.flagImage.image=[UIImage ImageName:@“jobPosted_opened.PNG”];
}否则{
cell.jobStatus.text=@“已关闭”;
[cell.jobStatus setTextColor:[UIColor color with red:(179/255.f)green:(179/255.f)blue:(180/255.f)alpha:1.0f];
cell.flagImage.image=[UIImage ImageName:@“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:indexath.row]stringValue];
返回单元;
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
以下是解决方案:
NSString
更改为NSArr
@property (strong, nonatomic) NSString* job_longitude;
@property (strong, nonatomic) NSString* job_latitude;
@property (strong, nonatomic) NSMutableDictionary* reversedGeocodes;
self.reversedGeocodes = [NSMutableDictionary dictionary];
- (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);
}
}
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;