1
votes

My service needs gps. I actually implemented a service, which turns the gps on, checks if the location is valid, and then goes to sleep for some time. (Starting with 5 seconds, and if no movement is detected, it can sleep up to a minute) After that, I start the gps again, and get a new location.

But the battery drain is still high! Ive used locMgr.distanceFilter = 10.0f and desiredAccurady = NearestTenMeters.

How do I minimize battery drain more?

1

1 Answers

1
votes

This is how I handle it. After I grab the location I store it in a NSDictionary. Then if I need to location again I return the NSDictionary instead of turning the GPS back on. After 2 minutes I reset the NSDictionary (you can adjust the time to what best suites you app). Then the next time I need the location after the NSDictionary was reset I grab a new location from the 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;
}