使用AFNetworking 3将当前位置发送到服务器以及在后台运行应用程序

我试图search很多,但我没有得到确切的答案,我想要的。

我的问题是

当应用程序在运行时以及在后台运行时,我可以将用户的当前位置发送到服务器吗?

例如:在我的应用程序中,我想每2分钟存储一次用户的当前位置,然后将其当前位置发送到server.as以及如果用户在2分钟内移动50米,那么也是如此。 我怎样才能做到这一点 ? 我可以发送位置更新到服务器在后台模式以及前台模式? 如果是的话那怎么样?

我尝试如下方式:

self.locations = [[NSMutableArray alloc] init]; self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.distanceFilter = 10; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.delegate = self; [self.locationManager requestAlwaysAuthorization]; [self.locationManager startUpdatingLocation]; 

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { // Add another annotation to the map. MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; annotation.coordinate = newLocation.coordinate; [self.map addAnnotation:annotation]; // Also add to our map so we can remove old values later [self.locations addObject:annotation]; // Remove values if the array is too big while (self.locations.count > 1) { annotation = [self.locations objectAtIndex:0]; [self.locations removeObjectAtIndex:0]; // Also remove from the map [self.map removeAnnotation:annotation]; } if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive) { // determine the region the points span so we can update our map's zoom. double maxLat = -91; double minLat = 91; double maxLon = -181; double minLon = 181; for (MKPointAnnotation *annotation in self.locations) { CLLocationCoordinate2D coordinate = annotation.coordinate; if (coordinate.latitude > maxLat) maxLat = coordinate.latitude; if (coordinate.latitude < minLat) minLat = coordinate.latitude; if (coordinate.longitude > maxLon) maxLon = coordinate.longitude; if (coordinate.longitude < minLon) minLon = coordinate.longitude; } MKCoordinateRegion region; region.span.latitudeDelta = (maxLat + 90) - (minLat + 90); region.span.longitudeDelta = (maxLon + 180) - (minLon + 180); _latitude1=[NSString stringWithFormat:@"Latitude:%f",newLocation.coordinate.latitude]; _longitude1=[NSString stringWithFormat:@"Longitude:%f",newLocation.coordinate.longitude]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.fetchtokenString1 = [NSString stringWithString:_latitude1]; appDelegate.fetchedriveridString1 = [NSString stringWithString:_longitude1]; NSLog(@"%@",_latitude1); NSLog(@"%@",_longitude1); // the center point is the average of the max and mins region.center.latitude = minLat + region.span.latitudeDelta / 2; region.center.longitude = minLon + region.span.longitudeDelta / 2; // Set the region of the map. [self.map setRegion:region animated:YES]; } else { NSLog(@"App is backgrounded. New location is %@", newLocation); } 

提前致谢..

当你的应用程序在前台运行时检查这个。

如何从iOS中的用户获取当前位置?

选中此项以在应用程序处于后台时发送位置信息。

当应用程序处于后台时,向服务器发送经纬度

得到经纬度后,使用AFNetworking框架发送。

要么

如果您需要从头开始,如果您不知道如何获取位置,请按照以下步骤获取经纬度。

在你的info.pList中添加下面的两个stringtypes

在这里输入图像说明

然后在您的项目中添加CoreLocation框架。

将其导入到您的ViewController.h

  #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController<CLLocationManagerDelegate> @property(nonatomic, retain) CLLocationManager *locationManager; @end 

然后在你的ViewController.m下面做一个

  #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; #ifdef __IPHONE_8_0 if(IS_OS_8_OR_LATER) { // Use one or the other, not both. Depending on what you put in info.plist [self.locationManager requestWhenInUseAuthorization]; [self.locationManager requestAlwaysAuthorization]; } #endif [self.locationManager startUpdatingLocation]; } -(void)viewWillAppear:(BOOL)animated { NSLog(@"latitude: %f, longitude: %f",self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } 

日志将显示经度和纬度。