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

时间:2016-05-20 05:20:25

标签: ios objective-c afnetworking

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

我的问题是

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

例如:在我的应用中,我希望每2分钟存储用户的当前位置,然后将其当前位置发送到服务器。如果用户在不到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);
}

提前致谢..

1 个答案:

答案 0 :(得分:0)

当您的应用在前台运行时检查此项。

How can I get current location from user in iOS

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

Sending Latitude and Longitude to Server when app is in background

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

OR

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

在info.pList

中添加以下两种字符串类型

enter image description here

然后在项目中添加CoreLocation Framework。

将其导入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.
}

日志将显示纬度和经度。