2013-05-16 62 views
1

由于我需要用户在后台的位置,我正在获取用户的位置在didUpdateLocation。但由于这样会消耗更多的电池。获取用户在后台的位置

我已经实施startMonitoringSignificantLocationChanges后台方法,但根据我didUpdateLocation不起作用。

任何解决方案,所以我可以消耗电池电量不足电源,也可以频繁地获取用户的更新位置。

+1

给一些你写的代码 – Durgaprasad

+0

self.locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; _locationManager.pausesLocationUpdatesAutomatically = NO; _locationManager.desiredAccuracy = kCLLocationAccuracyBest; [_locationManager startUpdatingLocation]; – Maddy

+0

在后台的方法[_locationManager startMonitoringSignificantLocationChanges] – Maddy

回答

0

“didUpdateLocation。但由于这样会消耗更多的电池。”

这并非完全正确。您可以使用didUpdateLocation调整所需的精度级别。这取决于你想要什么样的准确性,您的设备将只使用,而不是你的母语GPS接收机的蜂窝位置数据打开所有的时间,例如:

较低的电池消耗和500米的精度
CLLocationAccuracy myaccuracy = 500.00;

最大精度和最大电池引流可以有(需要在设备上的充电器被插入)
CLLocationAccuracy myaccuracy = -1.0;


如果你不知道要选择哪个值也可以使用常量:

的extern const的CLLocationAccuracy kCLLocationAccuracyBestForNavigation; < - 表示-1.0
extern const CLLocationAccuracy kCLLocationAccuracyBest;
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;

https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CoreLocationConstantsRef/Reference/reference.html#//apple_ref/c/data/kCLLocationAccuracyBest


现在回答你的问题:

didUpdateLocation只适用于当你问你的LocationManager到startUpdatingLocation。

当你使用startMonitoringSignificantLocationChanges时,你没有从你的GPS单元获得任何信息。使用这种方法时,一旦手机从一个手机塔变为另一个手机塔(例如乘火车或汽车旅行),您的应用程序将被唤醒。当你的应用被它唤醒,你需要实现:

id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]; 
if (locationValue) {  
    NSLog(@"locationValue received, now needs to start updateLocation again"); 
    //your code goes here 
} 

里面你

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

}

然后你就可以以接收didUpdateLocation通知重新开始你的正常startUpdatingLocation。或者,您可以查看Apple的文档,并查看launchOptions在应用程序启动时是否携带任何位置信息。