[英]update location in background in iOS
我正在研究步行轨道类型的应用程序,但面临的问题是,当应用程序在后台时,位置将如何上升。 我正在地图上绘制路径作为位置更新和计时器。 那么请建议我如何处理它。 这是我的代码
- (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
文件中设置/添加 Require background mode
非常重要。值是App Registers for location update
这样的
您可以使用重要更改位置服务来接收位置事件。
此服务可显着节省电力,并提供足以满足大多数应用程序的准确性。 它使用设备的蜂窝无线电来确定用户的位置并报告该位置的变化,从而使系统能够比其他方式更积极地管理用电量。 此服务还能够唤醒当前暂停或未运行的应用程序,以便提供新的位置数据。
要使用重要更改位置服务:
创建CLLocationManager类的实例,为其分配委托,并调用startMonitoringSignificantLocationChanges方法。 随着位置数据变得可用,位置管理器通知其分配的委托对象。 如果已经提供了位置更新,您还可以直接从CLLocationManager对象获取最新的位置数据,而无需等待传递新事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.