问题
i'm very new in iOS app development.I am going to use AFNetworking 3.0.I want to update location in background and send it to server. while send it to server,i want to condition: 1)if location not change i want to call service sendlocation1 2)if location change i want to call service sendlocation2. location change (near about 50 meters). Please help me.... i tried following code---- in ViewDidLoad:
(void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager requestAlwaysAuthorization];
float Lat = locationManager.location.coordinate.latitude;
float Long = locationManager.location.coordinate.longitude;
NSLog(@"Lat : %f Long : %f",Lat,Long);}
i'm going to check,if i get response from server is Login Success then only send location to server, thats why i use if-else statement.like following:
if ([_str isEqualToString:@"Login Success"]) {
UIAlertController *Loginalert= [UIAlertController
alertControllerWithTitle:@"Login Success"
message:@"This app is going to close now"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
if () {
[self sendlocation1];
} else {
[self sendlocation2];
}
[Loginalert dismissViewControllerAnimated:YES completion:nil];
}];
[Loginalert addAction: yesButton];
} else {
NSLog(@"Something Wrong");
and finally
(void)sendlocation1{
}
(void)sendlocation2{
}
please help me...how to check if location change or not? ...what should I write in if condition and send it to server.
回答1:
Step-1
(void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
//set the amount of metres travelled before location update is made
[locationManager setDistanceFilter:50];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager requestAlwaysAuthorization];
// call the timer with 5 minutes cap using 5 * 60 = 300
[NSTimer scheduledTimerWithTimeInterval:300.0f target:self selector:@selector(sendlocation1) userInfo:nil repeats:YES];
step-2
Every 50 Meter change Device This Method is called :
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%f",newLocation.coordinate.latitude);
NSLog(@"%f",newLocation.coordinate.longitude);
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
if(meters >=50)
{
// call webservice for location is updated
[self sendlocation1];
}else
{
// call normal method
[self webservice_UpdateLocation];
}
}
}
-(void)webservice_UpdateLocation
{
if ([_str isEqualToString:@"Login Success"]) {
UIAlertController *Loginalert= [UIAlertController
alertControllerWithTitle:@"Login Success"
message:@"This app is going to close now"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[self sendlocation2];
[Loginalert dismissViewControllerAnimated:YES completion:nil];
}];
[Loginalert addAction: yesButton];
} else {
NSLog(@"Something Wrong");
}
回答2:
You can check the displacement of the 2 (coord_A, coord_B) co ordinates using CLLocation.
CLLocationDistance distance = [coord_A distanceFromLocation:coord_B];
The distance you will receive will be in meters. You can execute your conditions in an If-Else loop with the obtained distance.
回答3:
Please check this answer for sending location to server for every n seconds
Sending Latitude and Longitude to Server when app is in background
来源:https://stackoverflow.com/questions/37315226/how-to-send-background-location-to-server-periodically-if-location-change-or-not