iOS: Get Coordinates on button click, then stop all location services -
right now, have when button clicked, starts location services, sets latitude/longitude variables. location still continues running in background.
i want when click button finds latitude/longitude, location services stop.
here's have currently.
- (ibaction)startbutton:(id)sender { locationmanager.delegate = self; locationmanager.desiredaccuracy = kcllocationaccuracybest; [locationmanager startupdatinglocation]; nslog(@"%@", [self lat]); } - (void)locationmanager:(cllocationmanager *)manager didfailwitherror:(nserror *)error { nslog(@"didfailwitherror: %@", error); uialertview *erroralert = [[uialertview alloc] initwithtitle:@"error" message:@"failed location" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [erroralert show]; } - (void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation { cllocation *currentlocation = newlocation; if (currentlocation != nil) { [self setlat:[nsstring stringwithformat:@"%.6f", currentlocation.coordinate.latitude]]; } }
another problem is, if instantly try retrieve value of lat property, returns null, because takes second or 2 find location.
so need stop further code run until lat/long properties set. example, how stop otherfunction
running until latitude/longitude found:
- (ibaction)startbutton:(id)sender { [self updatelocation]; [self otherfunction]; } - (void)updatelocation { locationmanager.delegate = self; locationmanager.desiredaccuracy = kcllocationaccuracybest; [locationmanager startupdatinglocation]; } - (void)locationmanager:(cllocationmanager *)manager didfailwitherror:(nserror *)error { nslog(@"didfailwitherror: %@", error); uialertview *erroralert = [[uialertview alloc] initwithtitle:@"error" message:@"failed location" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [erroralert show]; } - (void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation { cllocation *currentlocation = newlocation; if (currentlocation != nil) { [self setlat:[nsstring stringwithformat:@"%.6f", currentlocation.coordinate.latitude]]; [locationmanager stopupdatinglocation] } }
any help/ideas appreciated!
in last function, after verify currentlocation not nil, should check accuracy of currentlocation, call setlat: function, stop updating location. see post more details:
Comments
Post a Comment