2012-03-23 83 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

相關問題