我正在开发步行轨道类型的应用程序,但面临着当应用程序处于后台时位置将如何更新的问题。我在 map 上绘制一条路径作为位置更新和一个计时器。 那么请建议我如何处理它。 这是我的代码
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if(!newLocation) return;
if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
(oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
{
// to draw path as new location find
jogPoint = [[JogPoint alloc] initWithCenterCoordinate:newLocation.coordinate];
[jogPoints addObject:jogPoint];
if([jogPoints count] >= 5) {
[self.mapView addOverlay:jogPoint];
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
// returns distance in meters
distnc+=[loc1 distanceFromLocation:loc2] ;
distanceLabel.text=[NSString stringWithFormat:@"%lf",distnc];
// jogInfo.distance += ([loc1 distanceFromLocation:loc2]) * 0.000621371192;
// jogInfo.eclapsedTime = (CFAbsoluteTimeGetCurrent() - startTime) * 1000 * 60;
}
}
}
最佳答案
如果您的应用程序处于后台模式,位置也会像您的应用程序一样进行跟踪/更新。活跃所以不用担心:)
只要确保你创建的 CLLocationManager
就像
self.currentLocation = [[CLLocationManager alloc]init];
self.currentLocation.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.currentLocation.delegate = self;
[self.currentLocation startUpdatingLocation];
已编辑:
如果在此处设置 distanceFilter
,那么您的方法会在特定的仪表处调用,该值在 distanceFilter
中设置,否则它会在您的设备移动时更新
这对于设置/添加 需要后台模式
非常重要 YourProjectName-Info.plist
文件
值为 App Registers for location update
比如
关于ios - 在 iOS 后台更新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18756387/