如果位置更改或不使用AFN 3,如何定期向服务器发送背景位置?
我在iOS应用程序开发中是非常新的。我将使用AFNetworking 3.0。我想在后台更新位置并将其发送到服务器。 而发送到服务器,我想条件:1)如果位置不改变,我想调用服务sendlocation1 2)如果位置更改我想调用服务sendlocation2。 位置变化(约50米)。 请帮助我….我试着下面的代码—-在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);}
我要检查,如果我从服务器得到响应login成功,然后只发送位置到服务器,这就是为什么我使用if-else语句如下:
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");
最后
(void)sendlocation1{ } (void)sendlocation2{ }
请帮助我…如何检查位置是否改变? …如果条件并发送到服务器,我应该写什么。
步骤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];
第2步
每50米更换一次设备此方法称为:
-(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"); }
您可以使用CLLocation检查2(coord_A,coord_B)坐标的位移。
CLLocationDistance distance = [coord_A distanceFromLocation:coord_B];
您将收到的距离将以米为单位。 你可以用获得的距离在一个If-Else循环中执行你的条件。
请每隔n秒检查一次发送位置到服务器的答案
当应用程序处于后台时,向服务器发送经纬度