使用 AFNetworking 3 在后台以及 运行 应用程序中将当前位置发送到服务器
sending current location to server in background as well as in running app using AFNetworking 3
我试着搜索了很多,但没有得到我想要的确切答案。
我的问题是
当应用处于 运行 以及在后台运行时,我能否将用户的当前位置发送到服务器。
例如: 在我的应用程序中,我想每 2 分钟存储一次用户的当前位置,然后将他的当前位置发送到 server.as,就像用户移动 50 次一样米内不到2min便也。
我怎样才能做到这一点 ?我可以在后台模式和前台模式下向服务器发送位置更新吗?如果是那么怎么办?
我试过如下方式:
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);
}
提前致谢..
当您的应用 运行 在前台时检查此项。
How can I get current location from user in iOS
选中此项以在您的应用程序处于后台时发送位置。
获取经纬度后,使用AFNetworking框架发送。
或
如果您不知道如何获取位置而需要从头开始,请按照以下步骤获取经纬度。
在 info.pList
中的两种字符串类型下方添加
然后在您的项目中添加 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.
}
日志会显示经纬度。
我试着搜索了很多,但没有得到我想要的确切答案。
我的问题是
当应用处于 运行 以及在后台运行时,我能否将用户的当前位置发送到服务器。
例如: 在我的应用程序中,我想每 2 分钟存储一次用户的当前位置,然后将他的当前位置发送到 server.as,就像用户移动 50 次一样米内不到2min便也。 我怎样才能做到这一点 ?我可以在后台模式和前台模式下向服务器发送位置更新吗?如果是那么怎么办?
我试过如下方式:
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);
}
提前致谢..
当您的应用 运行 在前台时检查此项。
How can I get current location from user in iOS
选中此项以在您的应用程序处于后台时发送位置。
获取经纬度后,使用AFNetworking框架发送。
或
如果您不知道如何获取位置而需要从头开始,请按照以下步骤获取经纬度。
在 info.pList
中的两种字符串类型下方添加然后在您的项目中添加 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.
}
日志会显示经纬度。