我确实使用定位服务编写了一个应用,该应用必须每10秒发送一次位置。而且效果很好。
只需按照Apple的文档使用“ allowDeferredLocationUpdatesUntilTraveled:timeout ”方法即可。
我所做的是:
必需:注册后台模式以更新位置。
1.使用和根据需要创建LocationManger
和:startUpdatingLocation
accuracy
filteredDistance
-(void) initLocationManager
{
// Create the manager object
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
_locationManager.delegate = self;
// This is the most important property to set for the manager. It ultimately determines how the manager will
// attempt to acquire location and thus, the amount of power that will be consumed.
_locationManager.desiredAccuracy = 45;
_locationManager.distanceFilter = 100;
// Once configured, the location manager must be "started".
[_locationManager startUpdatingLocation];
}
2.为了使应用程序永远allowDeferredLocationUpdatesUntilTraveled:timeout
在后台运行,必须updatingLocation
在应用程序移至后台时使用新参数重新启动,如下所示:
- (void)applicationWillResignActive:(UIApplication *)application {
_isBackgroundMode = YES;
[_locationManager stopUpdatingLocation];
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[_locationManager setDistanceFilter:kCLDistanceFilterNone];
_locationManager.pausesLocationUpdatesAutomatically = NO;
_locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[_locationManager startUpdatingLocation];
}
3.应用程序通过locationManager:didUpdateLocations:
回调正常获取updatedLocations :
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// store data
CLLocation *newLocation = [locations lastObject];
self.userLocation = newLocation;
//tell the centralManager that you want to deferred this updatedLocation
if (_isBackgroundMode && !_deferringUpdates)
{
_deferringUpdates = YES;
[self.locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:10];
}
}
4.但是您应该locationManager:didFinishDeferredUpdatesWithError:
根据自己的目的在then 回调中处理数据
- (void) locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error {
_deferringUpdates = NO;
//do something
}
5. 注意:我认为我们应该在LocationManager
应用程序每次在背景/地面模式之间切换时重置参数。