iphone - CLLocationManager initialization and call on button -
i want app current location when user taps button. initialize locationmanager object in init method.
first question: if i'm going need currentlocation every time press button? or should initialize in viewdidload?
- (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // create location manager object locationmanager = [[cllocationmanager alloc] init]; locationmanager.delegate = self; // best accuracy posibble [locationmanager setdesiredaccuracy:kcllocationaccuracybest]; } return self; } in delegate method stopupdatinglocation got currentlocation.
// delegate method - (void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations { cllocation *currentlocation = [locations lastobject]; [locationmanager stopupdatinglocation]; } here startupdatinglocation button:
- (ibaction)getcurrentlocation:(id)sender { [locationmanager startupdatinglocation]; } second question is: when press button 1st time, right location, when change location in simulator , press again shows 1st one. again, when press button, shows right one. have tried initialize locationmanager every time button pressed doesn't work way neither.
i read on other post because of location cache or that. how remove cache ? because i'm going store location somewhere else in db anyways don't need old one..
question 1: should initialize location manager in init or in viewdidload
i go viewdidload, because way can sure dependencies have been loaded.
question 2: user location isn't being updated.
the user location isn't changing because stop listening location updates after receive location information first time:
[locationmanager stopupdatinglocation]; but start listening again when user presses getcurrentlocation button. can still go approach, if like, set flag in getcurrentlocation action update ui when new location found.
example:
// delegate method - (void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations { cllocation *currentlocation = [locations lastobject]; [locationmanager stopupdatinglocation]; if (self.updateuionnextlocation) { [self updateuiwithlocation:currentlocation]; self.updateuionnextlocation = no; } } ... - (ibaction)getcurrentlocation:(id)sender { self.updateuionnextlocation = yes; [locationmanager startupdatinglocation]; }
Comments
Post a Comment