iphone - CLLocationCoordinate2D currentCentre not returning lat/long -
cllocationcoordinate2d currentcentre; not returning lat/long when use method in viewdidload if use viewdidappear works lat/long coming somewhere else(not accurate). on viewdidload or viewdidapear execute [self querygoogleplaces:@"grocery"]; i'm new nice please
-(void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. //back button uibarbuttonitem *back = [[uibarbuttonitem alloc] initwithtitle:@"back" style:uibarbuttonitemstyleplain target:nil action:nil]; [[self navigationitem] setbackbarbuttonitem:back]; self.mapview.delegate = self; locationmanager = [[cllocationmanager alloc] init]; [locationmanager setdelegate:self]; [locationmanager setdistancefilter:kcldistancefilternone]; [locationmanager setdesiredaccuracy:kcllocationaccuracybest]; [self.mapview setshowsuserlocation:yes]; firstlaunch=yes; imagename=[[nsstring alloc] init]; imagename=[nsstring stringwithformat:@"park.png"]; [self querygoogleplaces:@"grocery"]; } -(void) querygoogleplaces: (nsstring *) googletype { // build url string going sent google. note: kgoogle_api_key constant should contain own api key can obtain google. see link more info: // https://developers.google.com/maps/documentation/places/#authentication nsstring *url = [nsstring stringwithformat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&keyword=%@&sensor=true&key=%@", currentcentre.latitude, currentcentre.longitude, [nsstring stringwithformat:@"%i", currendist], googletype, kgoogle_api_key]; nslog(@"test: %@",url); //formulate string url object. nsurl *googlerequesturl=[nsurl urlwithstring:url]; //nslog(url); // retrieve results of url. dispatch_async(kbgqueue, ^{ nsdata* data = [nsdata datawithcontentsofurl: googlerequesturl]; [self performselectoronmainthread:@selector(fetcheddata:) withobject:data waituntildone:yes]; });
}
my .h code
@interface mapviewcontroller : uiviewcontroller<mkmapviewdelegate,cllocationmanagerdelegate> { cllocationmanager *locationmanager; nsstring *imagename; bool firstlaunch; cllocationcoordinate2d currentcentre; int currendist; } @property (strong, nonatomic) iboutlet mkmapview *mapview; -(void) querygoogleplaces: (nsstring *) googletype; @end
but if call in button works so
- (ibaction)toolbarbuttonpresses:(id)sender { imagename=[[nsstring alloc] init]; imagename=[nsstring stringwithformat:@"park.png"]; [self querygoogleplaces:@"grocery"]; }
you need call [locationmanager startupdatinglocation];
in viewdidload
, check has returned valid location querying location property - checking isn't nil. might taking while return result, when rest of code executes there isn't valid value. might able use – locationmanager:didupdatetolocation:fromlocation: – locationmanager:didfailwitherror:
method in cllocationmanagerdelegate protocol handle this.
Comments
Post a Comment