获取用户在后台的位置

时间:2013-05-16 06:14:32

标签: iphone ios location core-location

因为我需要用户在后台的位置,我在didUpdateLocation获取用户的位置。但由于这个原因,它消耗更多的电池。

我在后台方法中实施了startMonitoringSignificantLocationChanges,但据我所知didUpdateLocation无效。

任何解决方案,以便我可以消耗低电量电源,并且还经常更新用户的位置。

1 个答案:

答案 0 :(得分: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启动更新位置时才有效。

使用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 {  

}

然后,您可以再次启动常规startUpdatingLocation以接收didUpdateLocation通知。或者,您可以查看Apple的文档,看看launchOptions是否在应用程序启动时携带任何位置信息。

相关问题