【问题标题】:sending current location to server in background as well as in running app using AFNetworking 3使用 AFNetworking 3 在后台以及正在运行的应用程序中将当前位置发送到服务器
【发布时间】:2016-09-17 05:36:24
【问题描述】:

我试图大量搜索这个,但我没有得到我想要的确切答案。

我的问题是

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

例如:在我的应用程序中,我想每 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);
}

提前谢谢..

【问题讨论】:

  • 你能发布你的代码吗..
  • 你的问题昨天没有解决
  • 这是我面临的另一个问题..@Anbu.Karthik...我想每 2 分钟以及(如果用户在 2分钟然后也)......你昨天的回答真的最好......谢谢......但这是我面临的新问题......请帮助我
  • @Anbu.Karthik....能不能聊聊这个问题?
  • 我可以知道为什么问题被否决了吗??

标签: ios objective-c afnetworking


【解决方案1】:

当您的应用在前台运行时选中此项。

How can I get current location from user in iOS

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

Sending Latitude and Longitude to Server when app is in background

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

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

在 info.pList 中添加以下两种字符串类型

然后在您的项目中添加 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.
}

日志会显示纬度和经度。

【讨论】:

  • 我在前景和背景中都获得了当前位置,兄弟......但请正确阅读我的问题......我想在位置变化时发送位置......以及如果位置没有变化从最后 2 分钟开始。然后还将位置发送到服务器.....@Santo
  • @SurajSukale 你说你甚至没有正确获得位置,所以我在上面发布了一个以获得 lat long
  • 我想每 2 分钟获取一次用户的当前位置,然后将他的当前位置发送到服务器。如果用户在不到 2 分钟内移动了 50 米,那么也是如此。我可以这样做吗? ..这是我的实际问题..请帮我解决这个问题兄弟....@Santo
  • 我发布了一个使用它的链接,并且每两分钟需要很长时间并比较它们,如果用户更改则更新或不更新。拿一个 NStimer 来检查它。使用 CLLocation 坐标距离方法。它会帮助你。放置时间间隔 2 分钟并更新位置
  • 这会给你距离,你可以用这个检查用户是否移动。 CLLocation *location2 = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1]; CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; CLLocationDistance 距离 = [位置距离FromLocation:location2]; distanceInKm = 距离/1000.0;
猜你喜欢
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 2016-07-08
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多