【发布时间】: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