iphone - Get New Location when App Enters Foreground -
i having difficulty implementing think should pretty simple capability. have button compares user's location set of lat/lon coordinates , displays alert telling user if "nearby" or "far away" location. works if app remains in foreground.
however, if app gets sent background, locationmanager stops updating user's location. if happens, when user brings app , presses "compare location button", app doesn't seem able user's new coordinates correctly.
adding receive location updates background modes seems work, know drain battery may rejected apple.
all want able have button wait accurate set of lat/lon coordinates before trying calculate distance between user , other set lat/lon coordinates. here have far:
the location button this:
-(void) locationbutton { nsstring *userlatitude =[(pdcappdelegate *)[uiapplication sharedapplication].delegate getuserlatitude]; nsstring *userlongitude =[(pdcappdelegate *)[uiapplication sharedapplication].delegate getuserlongitude]; nsstring *placelatitude = [[nsuserdefaults standarduserdefaults] stringforkey:@"savedlatitude"]; nsstring *placelongitude = [[nsuserdefaults standarduserdefaults] stringforkey:@"savedlongitude"]; nsstring *distanceurl = [nsstring stringwithformat:@"http://www.website.com/page.php? lat1=%@&lon1=%@&lat2=%@&lon2=%@",userlatitude, userlongitude, placelatitude, placelongitude]; nsdata *distanceurlresult = [nsdata datawithcontentsofurl:[nsurl urlwithstring:distanceurl]]; nsstring *distanceinfeet = [[nsstring alloc] initwithdata:distanceurlresult encoding:nsutf8stringencoding]; if ([distanceinfeet isequaltostring:@"1"]) { uialertview *alert = [[uialertview alloc] initwithtitle:@"you're near!" message:@"" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil]; [alert show]; return; } if ([distanceinfeet isequaltostring:@"0"]) { uialertview *alert = [[uialertview alloc] initwithtitle:@"you're far!" message:@"" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil]; [alert show]; return; } } the first few lines of locationbutton method userlatitude , userlongitude app delegate:
- (nsstring *)getusercoordinates { nsstring *usercoordinates = [nsstring stringwithformat:@"latitude: %f longitude: %f", locationmanager.location.coordinate.latitude, locationmanager.location.coordinate.longitude]; locationmanager = [[cllocationmanager alloc] init]; locationmanager.distancefilter = kcldistancefilternone; // whenever move locationmanager.desiredaccuracy = kcllocationaccuracyhundredmeters; // 100 m [locationmanager startupdatinglocation]; return usercoordinates; } - (nsstring *)getuserlatitude { nsstring *userlatitude = [nsstring stringwithformat:@"%f", locationmanager.location.coordinate.latitude]; return userlatitude; } - (nsstring *)getuserlongitude { nsstring *userlongitude = [nsstring stringwithformat:@"%f", locationmanager.location.coordinate.longitude]; return userlongitude; }
Comments
Post a Comment