objective c - iOS: How can i get location names of multiple longitudes and latitudes when data is coming from web service as a response? -
i need code. getting longitudes , latitudes response web service. need convert them location name , show in respective table view cell label.
following response, code , screen shot of view show converted city, country name, , correct code doing mistake.
#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]; // additional setup after loading 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 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 coordinates [ceo reversegeocodelocation:loc completionhandler:^(nsarray *placemarks, nserror *error) { clplacemark *placemark = [placemarks objectatindex:0]; nslog(@"placemark %@",placemark); //string hold address [[placemark.addressdictionary valueforkey:@"formattedaddresslines"] componentsjoinedbystring:@", "]; nslog(@"addressdictionary %@", placemark.addressdictionary); nsarray *ar = [placemark.addressdictionary objectforkey:@"locality"]; nslog(@"placemark %@",ar); // extract 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 display urgent image if ([[self.job_priority objectatindex:indexpath.row] isequaltostring:@"urgent"]) { cell.urgentlabel.hidden = no; } else { cell.urgentlabel.hidden = yes; } // condition whether job completed 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 resources can recreated. }
here's solution:
change
nsstring
nsarray
of theseproperties
:@property (strong, nonatomic) nsstring* job_longitude; @property (strong, nonatomic) nsstring* job_latitude;
add new
property
hold geocode results:@property (strong, nonatomic) nsmutabledictionary* reversedgeocodes;
set
reversedgeocodes
inviewdidload:
method:self.reversedgeocodes = [nsmutabledictionary dictionary];
add method reversed geocode (and save future):
- (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; // each pair of lat , lng creating unique key , saving result // don't have process same pair multiple times. // example: // lat = 23.039567; lng = 72.566004; // key = @"23.03956772.566004"; value 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); } }
call above method
cellforrowatindexpat:
method before returncell
: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;
p.s. let me know if missed or misspelled because typed of code on here...
Comments
Post a Comment