2012-03-23 113 views
1

我的服务需要gps。我实际上实施了一项服务,开启GPS,检查位置是否有效,然后进入睡眠一段时间。 (从5秒开始,如果没有检测到移动,它可以睡一分钟) 之后,我再次启动GPS,并获得一个新的位置。iOS GPS电池耗尽,如何让它更少排水?

但电池耗尽仍然很高!我用过locMgr.distanceFilter = 10.0f和desiredAccurady = NearestTenMeters。

如何最大限度地减少电池消耗更多?

回答

1

这是我如何处理它。在获取位置后,我将它存储在NSDictionary中。然后,如果我需要再次定位,则返回NSDictionary而不是重新打开GPS。 2分钟后,我重置NSDictionary(您可以调整时间,以适应你最好的套件)。然后下一次我需要NSDictionary重置后的位置,我从GPS获取一个新的位置。

- (NSDictionary *) getCurrentLocation { 

if (self.currentLocationDict == nil) { 
    self.currentLocationDict = [[NSMutableDictionary alloc] init]; 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation]; 

    CLLocation *myLocation = [locationManager location]; 

    [self.currentLocationDict setObject:[NSString stringWithFormat:@"%f", myLocation.coordinate.latitude] forKey:@"lat"]; 
    [self.currentLocationDict setObject:[NSString stringWithFormat:@"%f", myLocation.coordinate.longitude] forKey:@"lng"]; 
    [locationManager stopUpdatingLocation]; 
    [locationManager release]; 

    //Below timer is to help save battery by only getting new location only after 2 min has passed from the last time a new position was taken. Last location is saved in currentLocationDict 
    [NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(resetCurrentLocation) userInfo:nil repeats:NO]; 
} 

return self.currentLocationDict; 
} 

- (void) resetCurrentLocation { 
NSLog(@"reset"); 
[currentLocationDict release]; 
self.currentLocationDict = nil; 
} 
+0

这不完全是我想要的。需要更多关于电池消耗的见解。 – 2012-04-06 20:32:03

相关问题